SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Tricky CONCAT statement

    I got stuck with this statement.

    I have two tables:

    Author: author_id, author_name
    Wrote: book_id, author id

    I want to get book_id with all its authors in one field, concatenating all the authors' names.

    For instance, let's suppose we have

    Wrote:
    book_id author_id
    10 50
    10 51
    10 52

    Author:
    author_id author_name
    50 John
    51 Mike
    52 Lisa


    I want to make a select statement which brings:

    book_id Authors
    10 John, Mike, Lisa



    Please HELP!!!

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,017
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    use the GROUP_CONCAT function
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    Non-Member
    Join Date
    Apr 2004
    Location
    Miami, FL, USA
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Putting r937's suggestion into a query, you get:
    Code:
    SELECT w.book_id, GROUP_CONCAT(a.author_name) AS authors
    FROM Wrote AS w
    INNER JOIN Author AS a ON w.author_id = a.author_id
    GROUP BY w.book_id;


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •