Jump to content

Archived

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

curtis_b

Is it possible.... nested queries

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Thanks for the reply, if anyone has code examples to show, I would greatly appreciate it.

Curt

Share this post


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

Make any sense?

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.