Jump to content

[SOLVED] Two tables comparision in mysql php


blogfisher

Recommended Posts

I want to index each row fetch through mysql database in php. For example, i have a database having two tables Tbl1 and Tbl2. Now i fetch data as an array :

 

$rowTbl1 = mysql_fetch_array($Tbl1result);

$rowTbl2 = mysql_fetch_array($Tbl2result);

 

cnt1Tbl1 = $rowTbl1['cnt'];

cnt1Tbl2 = $rowTbl2['cnt'];

 

I want to compare first row's field 'cnt' with each row on Tbl2 data. How to do that ?

 

e.g. if (cnt1Tbl1 > cnt1Tbl2) {

        // Do something

    }

    else {

      // get 'cnt' of next row from $rowTbl2

      if (cnt1Tbl1 > cnt2Tbl2) {

        // Do something

      }

      else {

          // get 'cnt' of next row from $rowTbl2

          if (cnt1Tbl1 > cnt3Tbl2) {

            // Do something

          }

 

        }

    }

Link to comment
Share on other sites

SELECT * FROM `tbl1` JOIN `tbl2` ON (tbl1.cnt > tbl2.cnt) WHERE `whatever` = 'whatever'

 

basically this query will grab every row TO BEGIN WITH which tbl1.cnt is greater than tbl2.cnt

 

then, it will proceed with the WHERE clause on that result set.

Link to comment
Share on other sites

I dint understand it exactly...Russell...well, let me elaborate it a bit more...

 

Consider i fetched two arrays $rowTbl1 & $rowTbl2 from mysql database. Assume in both case we got different no. of rows. $rowTbl1 has 10 rows and $rowTbl2 has 15 rows. Now i want to compare each row of $rowTbl1 with each row of $rowTbl2. I want to do something like below :

 

for ($i = 0; $i<10; $i++) {

  for ($j = 0; $j<15; $i++) {

      if ($rowTbl1[$i].cnt > $rowTbl2[$j].cnt) {

        // do something

      }

  }

}

 

I'm not sure how to do it ?

 

Link to comment
Share on other sites

ok..

 

<?php
$q1 = mysql_query("SELECT * FROM `tbl1`");
$q2 = mysql_query("SELECT * FROM `tbl2`");
while ($tbl1 = mysql_fetch_assoc($q1)) {
  $t1[] = $tbl1;
}
while ($tbl2 = mysql_fetch_assoc($q1)) {
  $t2[] = $tbl2;
}
$lead = ((count($tbl1) <= count($tbl2))? $tbl1:$tbl2);
$foll = ((count($tbl1) >= count($tbl2))? $tbl1:$tbl2);
for ($i = 0; $i < count($lead); $i++) {
  for ($k = 0; $k < count($foll); $k++) {
    if ($lead[$i]['cnt'] > $foll[$i]['cnt']) {
      do something
    }
  }
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.