swatisonee Posted March 11, 2006 Share Posted March 11, 2006 hi,a. I have 2 Tables: Table A and Table B b. I have to select all records from Table A that donot appear in Table B . 1 field in both is common .c. I dont have a problem selecting data that appears in both tables where the same field is in common but the reverse is getting caught in a loop and i'm not able to figure out what the error is.d. The error is that the else statement gets executed. Would appreciate some guidance pleas.e . Thanks.Swati[code]$sqlb= "SELECT * FROM `Enq` WHERE Category`!= 'D' && `Category`!= 'A' && fid='$fid'";$resultb=mysql_query($sqlb); if ($myrowb=mysql_fetch_array($resultb)){do{$enqid=$myrowb["Enqid"];$sqlc="SELECT * FROM `order` WHERE `Enqid`= $enqid ";if ( !@mysql_query($resultc) ){do { printf("<tr><td><input type=\"radio\" name=\"choice\" value=%d><td> <font size=2 face=Tahoma color=blue>%d<td> <font size=2 face=Tahoma color=blue>%s<td> <font size=2 face=Tahoma color=blue>%s<td> </tr>", $myrowb["Enqid"], $myrowb["fid"], $myrowb["Company"], calculatedate($myrowb["Date"]) ); }while ($myrowc = mysql_fetch_array($resultc)); }}while ($myrowb= mysql_fetch_array($resultb)); }else { echo "Sorry, no records were found!"; }[/code] Quote Link to comment https://forums.phpfreaks.com/topic/4692-getting-caught-in-a-loop/ Share on other sites More sharing options...
Barand Posted March 12, 2006 Share Posted March 12, 2006 To find unmatched records use LEFT JOIN and search for results where the data in the right table is NULL (ie no data)[code]SELECT a.* FROM tableA a LEFT JOIN tableB bON a.common = b.commonWHERE b.common IS NULL[/code] Quote Link to comment https://forums.phpfreaks.com/topic/4692-getting-caught-in-a-loop/#findComment-16535 Share on other sites More sharing options...
sasa Posted March 12, 2006 Share Posted March 12, 2006 [!--quoteo(post=353947:date=Mar 11 2006, 05:37 PM:name=swatisonee)--][div class=\'quotetop\']QUOTE(swatisonee @ Mar 11 2006, 05:37 PM) [snapback]353947[/snapback][/div][div class=\'quotemain\'][!--quotec--]hi,a. I have 2 Tables: Table A and Table B b. I have to select all records from Table A that donot appear in Table B . 1 field in both is common .c. I dont have a problem selecting data that appears in both tables where the same field is in common but the reverse is getting caught in a loop and i'm not able to figure out what the error is.d. The error is that the else statement gets executed. Would appreciate some guidance pleas.e . Thanks.Swati[code]$sqlb= "SELECT * FROM `Enq` WHERE Category`!= 'D' && `Category`!= 'A' && fid='$fid'";$resultb=mysql_query($sqlb); if ($myrowb=mysql_fetch_array($resultb)){do{$enqid=$myrowb["Enqid"];$sqlc="SELECT * FROM `order` WHERE `Enqid`= $enqid ";if ( !@mysql_query($resultc) ) // <-- look this line{do { printf("<tr><td><input type=\"radio\" name=\"choice\" value=%d><td> <font size=2 face=Tahoma color=blue>%d<td> <font size=2 face=Tahoma color=blue>%s<td> <font size=2 face=Tahoma color=blue>%s<td> </tr>", $myrowb["Enqid"], $myrowb["fid"], $myrowb["Company"], calculatedate($myrowb["Date"]) ); }while ($myrowc = mysql_fetch_array($resultc)); }}while ($myrowb= mysql_fetch_array($resultb)); }else { echo "Sorry, no records were found!"; }[/code][/quote]look lin 9 change '$resultc' to '$sqlc' Quote Link to comment https://forums.phpfreaks.com/topic/4692-getting-caught-in-a-loop/#findComment-16739 Share on other sites More sharing options...
swatisonee Posted March 13, 2006 Author Share Posted March 13, 2006 Hi,Thanks Barand, Sasa. I struggled to get the Join going and after lots of trial and error got it to work - it did not work till i put backticks for the table names in the on clause and also on column name in the where clause. Thanks again.Swati Quote Link to comment https://forums.phpfreaks.com/topic/4692-getting-caught-in-a-loop/#findComment-16902 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.