I don't know how to do this and have trouble explaining it:)

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
themaxtor
Forum Newbie
Posts: 1
Joined: Mon Apr 16, 2007 10:57 am

I don't know how to do this and have trouble explaining it:)

Post by themaxtor »

Anyone suggest an effecient way to do this - I'm not sure what I'm supposed to try next:

I have a query that looks more or less like:

SELECT * FROM mytable WHERE primary_id IN ('3,4,7')

this select is generated from a select and the 3, 4 and 7 are generated dynamically based on the actual ids chose from the select list. Anyhow, I then query all the other tables based on this id as the foreign key.

Now here is the problem, how the heck do I display this data.
I know I can just loop through to show the contents of the query for "mytable" but I want to display the data like this:

mydata->name1
othertable->info1
othertable->info2
othertable->info3

mydata->name2
othertable->info1
othertable->info2
othertable->info3


So I want to display the title for example from my main table and then all the detail info from the other tables. This would be straight forward with a single query but since I have queried as above, I really don't know what I'm supposed to do to match the data properly

Can anyone help....
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

SQL JOINs are you friend, learn them, use them, love them.

You should be able to do this in one query and get all of your data at once, then loop through that single record set to display your stuff.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You can do this with with two queries as well... one query for the parents, the other for the children.

Loop the first result set (parents) and for each iteration, loop the second result set and match the ids in the second set that match the id of the parent.
Post Reply