0

I am wondering if it is possible to select a number of rows in a mysql table but display them all as one value separated by commas for instance.

So if I had a table

Name        Color
---------------------
A          Red
B          Blue
A          Green

I could select Color where Name = 'A'; but both values into one such as it would return

Red, Green

or

Green, Red

Instead of

Red
Green

Thanks

Jeff
  • 480
  • 1
  • 5
  • 17
  • Voting to close: possible duplicate of [MySQL Results as comma separated list](http://stackoverflow.com/questions/662207/mysql-results-as-comma-separated-list) This question is asked almost daily for just about every RDBMS flavor. – Paul Sasik Oct 24 '11 at 14:15

2 Answers2

4
SELECT name,
    GROUP_CONCAT(DISTINCT color
               ORDER BY color DESC SEPARATOR ',')
     FROM table
    GROUP BY name;

I have added the SEPARATOR keyword in case you want to display something other than a comma(,). A comma is the default. Click here for more details.

reggie
  • 13,313
  • 13
  • 41
  • 57
2

Use GROUP BY in combination with GROUP_CONCAT:

 SELECT Name, GROUP_CONCAT(COLOR) FROM yourtable GROUP BY Name; 

Query untested

Ocaso Protal
  • 19,362
  • 8
  • 76
  • 83