How to use GROUP BY to concatenate strings in MySQL?

前端 未结 6 654
慢半拍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:11

    SELECT id, GROUP_CONCAT( string SEPARATOR ' ') FROM table GROUP BY id
    

    More details here.

    From the link above, GROUP_CONCAT: This function returns a string result with the concatenated non-NULL values from a group. It returns NULL if there are no non-NULL values.

提交回复
热议问题