Summarizing a column to a DISTINCT value

This is a discussion on Summarizing a column to a DISTINCT value within the MySQL Database forums, part of the Database Forums category; Hey folks, I am just getting into MySQL and have run into a problem with subqueries. I am running MySQL ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-09-2006
worldcyclist@gmail.com
 
Posts: n/a
Default Summarizing a column to a DISTINCT value

Hey folks,
I am just getting into MySQL and have run into a problem with
subqueries.
I am running MySQL 4.0.21 and I am trying to CONCAT and I have no idea
where I'm tripping up...

The data looks like this....

+----+------------+---------+
| id | product | color |
+----+------------+---------+
| 1 | mixer | blue |
| 2 | mixer | green |
| 3 | mixer | yellow |
+----+------------+

I need to generate this....
+----+------------+---------+
| id | product | color |
+----+------------+---------+
| 1 | mixer | blue/green/yellow |
+----+------------+

In short, for each distinct product, generate a color field where the
all the choices are concatenated by a slash.

My apologies if this is remarkably simple. The real world query is
actually more brutal than this one which I have reduced for simplicity.

Sherabg

Reply With Quote
  #2 (permalink)  
Old 09-09-2006
Bill Karwin
 
Posts: n/a
Default Re: Summarizing a column to a DISTINCT value

worldcyclist@gmail.com wrote:
> In short, for each distinct product, generate a color field where the
> all the choices are concatenated by a slash.


You need MySQL 4.1 to do this. The solution is:

SELECT MIN(id), product,
GROUP_CONCAT(color ORDER BY color SEPARATOR '/')
FROM tablename
GROUP BY product;

See documentation on GROUP_CONCAT() here:
http://dev.mysql.com/doc/refman/4.1/...functions.html

There is no alternative for GROUP_CONCAT() in MySQL 4.0. If you can't
upgrade, you'll have to fetch all the data and do the grouping in
application code.

Regards,
Bill K.
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 12:52 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0