Jump to content


Photo

Loops within loops


  • Please log in to reply
2 replies to this topic

#1 slack

slack
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 02 April 2006 - 11:34 PM

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.

TABLE 1
CALL_ID -------- NAME

1 ------------------- Mike
2 ------------------- John
3 ------------------- Kelly


TABLE 2
ID ------------ CALL_ID

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.


NAME------------ ACTION
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

#2 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 02 April 2006 - 11:52 PM

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, 'NO', 'YES') AS `Action`
FROM table1 LEFT JOIN table2
ON table1.CALL_ID = 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]

#3 slack

slack
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 03 April 2006 - 06:58 PM

THANKS. Your advice has worked.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users