Mysql how to smooth a result set

I have a stored procedure that will return a list of identifiers. I want to return this list as a comma separated string, i.e. "1,2,3,4,5".

I know I can do this with the cursor, but is there an easier way to turn a result set into a flattened string?

+4
source share
3 answers

MySQL has a group function group_concat () :

SELECT group_concat(some_column) FROM mytable; 

some_column all some_column values ​​from a table joined by commas.

Caution: Beware that the result is limited to the group_concat_max_len system variable, which defaults to 1024 bytes! To avoid hitting this wall, you must complete this before running the request:

 SET SESSION group_concat_max_len = 65536; 

Or more, depending on how many expected results. But this value cannot be greater than max_allowed_packet

+12
source

You must really format the external code.

-2
source

All Articles