How to use GROUP BY to concatenate strings in MySQL?

前端 未结 6 674
慢半拍i
慢半拍i 2020-11-22 03:56

Basically the question is how to get from this:

foo_id   foo_name
1        A
1        B
2        C

to this:

foo_id   foo_name
1        A B
2         


        
6条回答
  •  借酒劲吻你
    2020-11-22 04:21

    The result is truncated to the maximum length that is given by the group_concat_max_len system variable, which has a default value of 1024 characters, so we first do:

    SET group_concat_max_len=100000000;
    

    and then, for example:

    SELECT pub_id,GROUP_CONCAT(cate_id SEPARATOR ' ') FROM book_mast GROUP BY pub_id
    

提交回复
热议问题