Get the book

Go Back   Zen Cart Support > General Questions > General Questions

General Questions Questions that don't fit elsewhere

Reply
 
Thread Tools Display Modes
Old 17th May 2006, 05:35 PM   #1
angel17846
New Zenner
 
Join Date: Dec 2005
Posts: 55
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
angel17846 is offline   Reply With Quote
Old 17th May 2006, 06:09 PM   #2
angel17846
New Zenner
 
Join Date: Dec 2005
Posts: 55
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;
angel17846 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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


All times are GMT +1. The time now is 02:14 PM.

Learn tips, tricks & secrets for your Zen Cart™
Sign up for our FREE Newsletter

Powered by vBulletin® Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content and Graphics Copyright (c) 2006, 2007, 2008, 2009, 2010 Zen Ventures, LLC - all rights reserved
Get Zen Cart E-Commerce Shopping Cart at SourceForge.net. Fast, secure and Free Open Source software downloads