Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I concatenate set of results in MySQL?

I would like to join results returned in the set in MySQL with a comma as a separator string.

For example, set returned contains:

COLUMN_X john jerry maria joseph gugla 

I would like to receive the result as:

COLUMN_X-concat john,jerry,maria,joseph,gugla 

is that possible? thanks.

SELECT CONCAT(rooms.ID,",") FROM rooms AS rooms LEFT JOIN inter AS i ON rooms.ID=i.value WHERE xxx=999 

doesn't work as I would like it to as it returns separate results.

like image 738
dusoft Avatar asked Nov 13 '09 10:11

dusoft


People also ask

Can I concatenate multiple MySQL rows into one field?

The GROUP_CONCAT() function in MySQL is used to concatenate data from multiple rows into one field. This is an aggregate (GROUP BY) function which returns a String value, if the group contains at least one non-NULL value.

How do I concatenate a query in MySQL?

CONCAT() function in MySQL is used to concatenating the given arguments. It may have one or more arguments. If all arguments are nonbinary strings, the result is a nonbinary string. If the arguments include any binary strings, the result is a binary string.


1 Answers

SELECT GROUP_CONCAT(COLUMN_X SEPARATOR ',') FROM <<table>> GROUP BY NULL 

See GROUP_CONCAT.

like image 171
Stefan Gehrig Avatar answered Sep 23 '22 10:09

Stefan Gehrig