How to use GROUP BY to concatenate strings in MySQL?

Basically the question is how to get from this:

id    string
1          A
1          B
2          C

to this:

id    string
1          A B
2          C

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

http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

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.


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.


SELECT id, GROUP_CONCAT(CAST(string as CHAR)) FROM table GROUP BY id

会给你一个逗号分隔的字符串

链接地址: http://www.djcxy.com/p/24852.html

上一篇: 如何连接一个std :: string和一个int?

下一篇: 如何使用GROUP BY连接MySQL中的字符串?