Jump to content

Archived

This topic is now archived and is closed to further replies.

slack

Loops within loops

Recommended Posts

Hi all,

I have attempted to do this but I can't seem to get the results I need. Here is the scenario:

I have two tables TABLE 1 and TABLE 2. Notice that CALL_ID in TABLE 2 is referencing CALL_ID in TABLE 1.

[b]TABLE 1
CALL_ID -------- NAME[/b]
1 ------------------- Mike
2 ------------------- John
3 ------------------- Kelly


[b]TABLE 2
ID ------------ CALL_ID [/b]
1 ---------------- 1
2 ---------------- 1
3 ----------------- 3


Info is to be displayed like below. So basically if that persons CALL_ID is in TABLE 2 then it should say YES.


[b]NAME------------ ACTION[/b]
Mike --------------- YES
John --------------- NO
Kelly --------------- YES


I know I need some sort of loop that loops through TABLE 1 and for every entry it should check to see if its CALL_ID matches a CALL_ID in TABLE 2.

I know that this may be a bit to ask but could somebody please post the code for the loops to get the display I want.

Thank you
Slack

Share this post


Link to post
Share on other sites
You don't need two queries, just one. Example:

[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] table1.NAME, IF(table2.CALL_ID IS NULL, [color=red]'NO'[/color], [color=red]'YES'[/color]) [color=green]AS[/color] `Action`
[color=green]FROM[/color] [color=orange]table1[/color] [color=green]LEFT[/color] [color=green]JOIN[/color] table2
ON table1.CALL_ID [color=orange]=[/color] table2.CALL_ID [!--sql2--][/div][!--sql3--]

Join tutorial/examples:
[a href=\"http://www.w3schools.com/sql/sql_join.asp\" target=\"_blank\"]http://www.w3schools.com/sql/sql_join.asp[/a]

[a href=\"http://dev.mysql.com/doc/refman/4.1/en/control-flow-functions.html\" target=\"_blank\"]http://dev.mysql.com/doc/refman/4.1/en/con...-functions.html[/a]

Share this post


Link to post
Share on other sites

×

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.