comma separated string of selected values in mysql

前端 未结 8 558
梦如初夏
梦如初夏 2020-11-28 08:09

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;


        
相关标签:
8条回答
  • 2020-11-28 08:38

    If you have multiple rows for parent_id.

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

    If you want to replace space with comma.

    SELECT REPLACE(id,' ',',') FROM table_level where parent_id=4;
    
    0 讨论(0)
  • 2020-11-28 08:39

    First to set group_concat_max_len, otherwise it will not give you all the result:

    SET GLOBAL  group_concat_max_len = 999999;
    SELECT GROUP_CONCAT(id)  FROM table_level where parent_id=4 group by parent_id;
    
    0 讨论(0)
提交回复
热议问题