0

我有

ID username group
1  philip   football
2  george   baseball
3  alice    football
4  ani      football
5  george   football

并试图做到这一点

username   group
philip     football
george     baseball, football
alice      football
ani        football

换句话说,我想将 grop idetms 组合成一个字符串,例如“baysboll, football”

4

2 回答 2

3

你需要这个GROUP_CONCAT功能

 select username, GROUP_CONCAT(group) FROM table GROUP BY Username
于 2012-08-07T12:58:52.783 回答
3

不要忘记Group用反引号转义列以避免语法错误。GROUP是一个保留关键字

SELECT username, GROUP_CONCAT(`group`) 
FROM table
GROUP BY userName
于 2012-08-07T13:01:42.703 回答