Zen Cart Logo
Forums / General Questions / concatenate rows into one column

concatenate rows into one column

Locked

Views: 7,311

Results 1 to 2 of 2
This thread is locked. New replies are disabled.
17 May 2006, 3:35 PM
#1
angel17846 avatar

angel17846

New Zenner

Join Date:
Dec 2005
Posts:
57
Plugin Contributions:
0

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

17 May 2006, 4:09 PM
#2
angel17846 avatar

angel17846

New Zenner

Join Date:
Dec 2005
Posts:
57
Plugin Contributions:
0

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;