[SOLVED] Comma separated string of selected values in MySQL

Issue

I want to convert selected values into a comma separated string in MySQL.

My initial code is as follows:

SELECT id
FROM table_level
WHERE parent_id = 4;

Which produces:

'5'
'6'
'9'
'10'
'12'
'14'
'15'
'17'
'18'
'779'

My desired output would look like this:

"5,6,9,10,12,14,15,17,18,779"

Solution

Check this:

SELECT GROUP_CONCAT(id)
FROM table_level
WHERE parent_id = 4
GROUP BY parent_id;

Answered By – naveen goyal

Answer Checked By – Pedro (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.