Link to home
Start Free TrialLog in
Avatar of harishsai
harishsai

asked on

how to compare two tables in a db?

I need to compare 2 resultset from query($result1 & $ result2) to find a data which has same values.
my coding is as follows
 while($row1 = mysql_fetch_array($result1)){
      while(($row2 = mysql_fetch_array($result2)){
          if($row1["dbb"] == $row2["dbb"]{
           echo $row1["dbb"];
          }//if
      }//while
  }//while

By this coding I cannot get my required result.
can anyone help me out to solve the above problem.

thankx in advance.
harish
Avatar of dkjariwala
dkjariwala

What you are trying to achieve ?

I guess you can do this using query only if two tables,[queries] have common field.

Give some more details,we can have some elegant solution out.

JD
Avatar of harishsai

ASKER

Hai JD,
thanks for ur interest.kindly do help me.

my coding starts like this .....

$w1="Pass";
$result1 = mysql_query("select * from $tableName1
    where result='$w1'");
$result2=mysql_query("select * from $tableName2
    where result ='$w1'");
$num1=mysql_num_rows($result1);
$num2=mysql_num_rows($result2);
while($row1 = mysql_fetch_array($result1)){
     while(($row2 = mysql_fetch_array($result2)){
         if($row1["dbb"] == $row2["dbb"]{
          echo $row1["dbb"];
         }//if
     }//while
 }//while

ie what Iam trying to do is get $result1 & $result2 from 2 different tables where the condition is "Pass".

then from these 2 recordset($result1 & $result2) i need to get the record where field "dbb" has same value.

I hope now u can understand.
if not reply me back
thank u very much
harish
ie , $result1 & $result2 has common field "dbb".
ASKER CERTIFIED SOLUTION
Avatar of dkjariwala
dkjariwala

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
dkjariwala answer looks perfect but if dbb is a unique identifier where only one dbb record of table 1 matches only 1 record of table 2 you can do it all in the query


$Query = "select * from TableName1,TableName2 where TableName1.result = \"Pass\" And TableName2.result = \"Pass\" And Table1.dbb = Table2.dbb";

$result1 = mysql_query($Query);


   while($row = mysql_fetch_array($result1)){
               echo $row["dbb"];
       }


$row will contain all fields for record for both tables.
Yeah, exactly,I was goin to suggest that only.
You can do it using query and that would be best thing to do.

JD
hai JD,

Thank u very much.
It works wonderful.I could get exactly what I require.

but if I wanted it to write to another table (table3) in the same database("db1").ie to write $value1 in table3 under field name field1.
how am I to do?

thankx
harish