www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Server-Side Development > SQL

    SQL For all Structured Query Language, and general database questions.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1  
    Old 11-03-2009, 03:22 PM
    tarsus tarsus is offline
    Registered User
     
    Join Date: Mar 2005
    Posts: 242
    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
    Clearly, the first step is the GROUP BY the personID. But I know of no aggregate function that will serve the purpose of concatenating the "hobby" field from all the records associated with a particular personID. CONCAT does not work like this; likewise, there are several aggregate functions that will pull one select value from the foreign records (like MIN and MAX), but none I can see that will combine them.

    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.
    Reply With Quote
      #2  
    Old 11-03-2009, 03:34 PM
    tarsus tarsus is offline
    Registered User
     
    Join Date: Mar 2005
    Posts: 242
    Hold it! Turns out there is an aggregate function to do exactly that: GROUP_CONCAT. I could swear I've searched up and down for this before; how did I not know?

    Oh well, perhaps I'll save someone else the trouble:

    SELECT p.name, GROUP_CONCAT(h.hobby SEPARATOR ", ") AS hobbies
    FROM people p LEFT JOIN people_hobbies h ON p.personId=h.personId
    GROUP BY p.personID
    ORDER BY p.name
    Reply With Quote
    Reply

    Bookmarks

    Tags
    aggregate, foreign, sql


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 06:47 AM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.