SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    combining multiple rows into one

    Hi,

    I'm sure this is simple, but it's eluding me as many things do as a newbie. Anyway, I've been having trouble in my PHP code so I'm trying to at least get the right MySQL query, and this works for the info I need:

    SELECT artist.id, medium FROM artist, medium, artistmedium WHERE artist.id=medartid AND medium.id=mediumid

    and it gives me a very nice table with artist id #s on the left, and the medium they work in on the right. When one artist works in more than one medium, then they have more than one row like so:

    ID medium
    -- ----------
    1 | painting
    2 | sculpture
    2 | computer web/based
    3 | ceramics
    4 | painting
    4 | drawing
    4 | installation
    .
    .
    .
    and so on.

    What I would like to have is something like this:
    ID medium
    -- ----------
    1 | painting
    2 | sculpture, computer web/based
    3 | ceramics
    4 | painting, drawing, installation
    .
    .
    .

    How can I do this?

    Thanks,
    Satre

  2. #2
    SitePoint Evangelist pompopom's Avatar
    Join Date
    Feb 2004
    Location
    Huldenberg (Belgium)
    Posts
    426
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is more an SQL question rather then PHP

    look at the MySQL section and ask for a JOIN query...

    I'm not to good at these myself, otherwise I would have given you the correct query instead

    succes
    The Path of excess leeds to the tower of wisdom (W. Blake)

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,327
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Code:
    select artist.id
         , group_concat(medium.medium) as media
      from artist
    inner
      join artistmedium 
        on artistmedium.medartid = artist.id
    inner
      join medium
        on medium.id=artistmedium.mediumid
    group
        by artist.id
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks r937

    I tried that, and got an error (I had actually tried the GROUP_CONCAT command before and got an error too)

    #1064 - You have an error in your SQL syntax.**Check the manual that corresponds to your MySQL server version for the right syntax to use near '(medium.medium) AS media FROM artist INNER JOIN artistmedium ON

  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    combining multiple rows into one

    Hi,

    I'm sure this is simple, but it's eluding me as many things do as a newbie. Anyway, I've been having trouble in my PHP code so I'm trying to at least get the right MySQL query, and this works for the info I need:

    SELECT artist.id, medium FROM artist, medium, artistmedium WHERE artist.id=medartid AND medium.id=mediumid

    and it gives me a very nice table with artist id #s on the left, and the medium they work in on the right. When one artist works in more than one medium, then they have more than one row like so:

    ID medium
    -- ----------
    1 | painting
    2 | sculpture
    2 | computer web/based
    3 | ceramics
    4 | painting
    4 | drawing
    4 | installation
    .
    .
    .
    and so on.

    What I would like to have is something like this:
    ID medium
    -- ----------
    1 | painting
    2 | sculpture, computer web/based
    3 | ceramics
    4 | painting, drawing, installation
    .
    .
    .

    How can I do this?

    Thanks,
    Satre

  6. #6
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    requires mysql 4.1 or newer:
    Code:
    SELECT artist.id
         , group_concat(medium)
      FROM artist
         , medium
         , artistmedium
     WHERE artist.id=medartid
       AND medium.id=mediumid
    GROUP
         BY artist.id

  7. #7
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,327
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    well, it doesn't like your GROUP_CONCAT, which was introduced in version 4.1

    your choices: upgrade from the grandfather version you're on, to at least the father version (4.1) -- the current version is 5.0 -- or alternatively, collapse multiple rows for each artist into one row using php
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  8. #8
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,327
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    sorry, longneck, as you can see, there was a cross-post here, and i've merged two threads

    well, not exactly a cross-post, since satre originally posted in the php forum and was advised to try the mysql forum

    for future reference, folks, we move threads, we don't start identical new ones -- precisely for this reason, so that people do not spend time answering a thread that was already answered elsewhere
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  9. #9
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    ack. i was wondering what was going on here.


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
  •