blogfisher Posted March 8, 2009 Share Posted March 8, 2009 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 } } } Quote Link to comment https://forums.phpfreaks.com/topic/148454-solved-two-tables-comparision-in-mysql-php/ Share on other sites More sharing options...
RussellReal Posted March 8, 2009 Share Posted March 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/148454-solved-two-tables-comparision-in-mysql-php/#findComment-779468 Share on other sites More sharing options...
blogfisher Posted March 8, 2009 Author Share Posted March 8, 2009 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/148454-solved-two-tables-comparision-in-mysql-php/#findComment-779475 Share on other sites More sharing options...
RussellReal Posted March 8, 2009 Share Posted March 8, 2009 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 } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/148454-solved-two-tables-comparision-in-mysql-php/#findComment-779484 Share on other sites More sharing options...
blogfisher Posted March 8, 2009 Author Share Posted March 8, 2009 Hey Russell... u rock... it works fine... I highly appreciate your quick and correct response.. thanks a lot... Quote Link to comment https://forums.phpfreaks.com/topic/148454-solved-two-tables-comparision-in-mysql-php/#findComment-779486 Share on other sites More sharing options...
RussellReal Posted March 8, 2009 Share Posted March 8, 2009 anytime bro, click 'solved' so nobody else here comes and checks dis post out or w\e, so u can save us helpers a lil more time, nah mean. Ty Quote Link to comment https://forums.phpfreaks.com/topic/148454-solved-two-tables-comparision-in-mysql-php/#findComment-779494 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.