Jump to content


Photo

Is it possible.... nested queries


  • Please log in to reply
3 replies to this topic

#1 curtis_b

curtis_b
  • Members
  • PipPipPip
  • Advanced Member
  • 48 posts

Posted 24 March 2006 - 05:01 PM

I'm wondering if this is even possible, and if so, can someone point me in the direction of a good example....

Once a select query has been sent to mysql and in the while (fetch array) loop, I want to then send another select query/while loop to a different mysql table based on the current row value for a column in the first query. Sorry if that sounded confusing I don't know how else to explain it. The resulting output I'm trying to acheive would have the following structure:

ROW FROM ORIGINAL QUERY
-possible match/related row from nested, 2nd query
ROW FROM ORIGINAL QUERY
-possible match/related row from nested, 2nd query
-possible match/related row from nested, 2nd query
-possible match/related row from nested, 2nd query
-possible match/related row from nested, 2nd query
ROW FROM ORIGINAL QUERY
ROW FROM ORIGINAL QUERY
-possible match/related row from nested, 2nd query
ROW FROM ORIGINAL QUERY
-possible match/related row from nested, 2nd query
-possible match/related row from nested, 2nd query

Thanks for your help

#2 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 24 March 2006 - 05:57 PM

Yes, it's possible, but I wouldn't recommend it.

Use a WHERE clause to limit the mysql return to only what you want, then use a join, left or right depending, to retrieve all of the information in one query.

#3 curtis_b

curtis_b
  • Members
  • PipPipPip
  • Advanced Member
  • 48 posts

Posted 24 March 2006 - 06:08 PM

Thanks for the reply, if anyone has code examples to show, I would greatly appreciate it.

Curt

#4 keeB

keeB
  • Staff Alumni
  • Advanced Member
  • 1,078 posts
  • LocationCalifornia

Posted 24 March 2006 - 07:00 PM

[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] * from tblTable1 tt1
INNER JOIN tblTable2 tt2 on tt2.someID = tt1.someID
WHERE tt2.someID = 2
[!--sql2--][/div][!--sql3--]

Make any sense?

Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users