user1148875
user1148875

Reputation: 459

how to compare two tables in mysql and php?

its me with another trouble!

Table1

Table2

Query1

Query2

I need to compare this tables and if Table1 == Table2 echo "found". So i made 2 diferent querys and Im doing it like this:

while($row1 = mysql_fetch_array($Query1))
{
while($row2 = mysql_fetch_array($Query2))
{
if($row1['n'] == $row2['n'] )
{
echo 'found';
}
}
}

Kinda dumb? :\ cause it seems show just the 1st result and stop.

Thanks

EDIT

Exmpl: I got this table: clients, and table: VIP clients. I need to search on table VIP clients if there is any client with same id, and result an echo: "found it"

Upvotes: 0

Views: 3367

Answers (2)

sel
sel

Reputation: 4957

Not sure if this is what you want, but you could do it in one query to see if there is any matching records in vipclients.

select a.*,b.* ,CASE WHEN b.clientid IS NOT NULL 
       THEN 'FOUND'
       ELSE 'NOT FOUND'
END AS vipexists 
from clients a left outer join vipclients b on a.clientid=b.clientid

Upvotes: 1

lusketeer
lusketeer

Reputation: 1930

If I understand you correctly, you wanna find if same data exist in both table. You can store the result into two different arrays

$table1 = array();
$table2 = array();
while ($row = mysql_query($result1)){
  $table1[] = $row[0];
}
while ($row = mysql_query($result2)){
  $table2[] = $row[0];
}

And then use array_intersect() to find the intersection of the two

$intersect = array_intersect($table1, $table2);
echo count($intersect) > 0 ? "Found" : "Not Found";

Upvotes: 0

Related Questions