|
|||||||
| SQL For all Structured Query Language, and general database questions. |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Retrieve multiple foreign fields as aggregate field?
I hope the thread subject conveys some semblance of what I'm asking. In my data structures I very frequently have a situation like this example:
Table "people": field "personId" field "name" Table "people_hobbies": field "personID" field "hobby" (In real life, there would actually be a "hobbies" table and "people_hobbies" would contain two foreign keys, but I'm keeping it simple as an example.) What I want is to make a single query and get all people's names and a comma-delimited list of their hobbies. I.e.: Code:
Name Hobbies Charlie Reading, Art Karen Scuba Diving, Rock Climbing, Horseback Riding Dave Video Games, Movies What I always must do, then, is use a query that retrieves one record per hobby (in this example): SELECT p.name, h.hobby FROM people p LEFT JOIN people_hobbies h ON p.personID=h.personID ORDER BY p.personID And then use programming logic to do one loop through the results and build my own array with the foreign field concatenated. Anyone know of a way to do this with one query? Last edited by tarsus; 11-03-2009 at 03:25 PM. |
| Bookmarks |
| Tags |
| aggregate, foreign, sql |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|