Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2005
    Posts
    57
    Plugin Contributions
    0

    Default concatenate rows into one column

    I am running MySQL 5 and have a SQL query question...

    I have a table with data and structure like this:

    Col_1 Col_2
    1 black
    2 red
    2 blue
    2 pink
    3 white
    3 purple
    3 gray
    3 peach
    4 orange
    5 off white
    5 gold

    I'd like to concatenate the rows by id and separate Col_2 with commas for a result like this.

    Col_1 Col_2
    1 black
    2 red, blue, pink
    3 white, purple, gray, peach
    4 orange
    5 off white, gold

    Can someone help me with the query or function to make this happen?

    Thanks,
    Angel

  2. #2
    Join Date
    Dec 2005
    Posts
    57
    Plugin Contributions
    0

    Default Re: concatenate rows into one column

    I figured this out on my own. It can be done like this:

    SELECT b,
    GROUP_CONCAT(DISTINCT a
    ORDER BY a DESC SEPARATOR ', ')
    FROM table_name
    GROUP BY b;

 

 

Similar Threads

  1. v139h Help Getting Image Attributes into Rows
    By ptowers49 in forum Templates, Stylesheets, Page Layout
    Replies: 6
    Last Post: 5 Jan 2014, 06:54 PM
  2. How to add rows to column layout grid?
    By mysafetymarket in forum Templates, Stylesheets, Page Layout
    Replies: 3
    Last Post: 8 Jul 2010, 05:08 AM
  3. How to change 2 column website into 3 column? - Acadame
    By chris32882 in forum Addon Templates
    Replies: 11
    Last Post: 21 Dec 2008, 03:26 AM
  4. column VS. rows
    By Rob905 in forum Templates, Stylesheets, Page Layout
    Replies: 0
    Last Post: 10 Oct 2008, 03:05 PM
  5. Column vs Rows
    By tajul_ashyqin in forum All Other Contributions/Addons
    Replies: 6
    Last Post: 2 Jul 2006, 04:24 PM

Posting Permissions

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