tags:

views:

53

answers:

1

Let's say I have

SELECT name 
FROM table

which gives me something like

foo
bar
baz

and I need to join all the values, to make it into one string like

foo bar baz

so I guess it would be something like

foreach row in (select ...) 
   var += row

but I have absolutely no idea how to write it in MySQL.

+4  A: 

Use GROUP_CONCAT

SELECT GROUP_CONCAT(bar) FROM TABLE GROUP BY foo;
Paul Dixon
That will return NULL if any of the values are null. You might want to CONCAT each one and run ISNULL(foo, 'NULL') on each value to ensure you get results.
databyss
Or simply add a WHERE clause to exclude nulls.
Paul Dixon
Agreed, it depends on the requirements.
databyss