Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

how to compare two tables in a db?

Posted on 2002-03-21
7
185 Views
Last Modified: 2012-05-04
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
0
Comment
Question by:harishsai
  • 3
  • 3
7 Comments
 
LVL 5

Expert Comment

by:dkjariwala
ID: 6885077
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
0
 

Author Comment

by:harishsai
ID: 6885098
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
0
 

Author Comment

by:harishsai
ID: 6885102
ie , $result1 & $result2 has common field "dbb".
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 5

Accepted Solution

by:
dkjariwala earned 40 total points
ID: 6885184
$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

What happens is when the first  inner loop ( $row2 one ) gets over, the result set is set to end. and you will not be able to traverse it again.

So try like

$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);
$i = 0;
while($row1 = mysql_fetch_array($result1)
{
     $res_arr1[$i] = $row1;
     $i++;
}
$i=0;
while($row1 = mysql_fetch_array($result1)
{
     $res_arr2[$i] = $row2;
     $i++;
}

for($i=0;$i<num1;$i++)
{
     $row_result1 = $res_arr1[$i];
     $value1 = $row_result1['dbb'];

     for($j=0;$j<$num2;$j++)
     {
          $row_result2 = $res_arr2[$j];
          $value2= $row_result2['dbb'];
          if($value2 == $value1)
               print "Value matched. Matched value is $value1";
     }
}

I have not tested it,
But it should work,
JD
0
 
LVL 5

Expert Comment

by:andriv
ID: 6885255
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.
0
 
LVL 5

Expert Comment

by:dkjariwala
ID: 6885440
Yeah, exactly,I was goin to suggest that only.
You can do it using query and that would be best thing to do.

JD
0
 

Author Comment

by:harishsai
ID: 6887465
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
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question