简体   繁体   English

将类似的查询结果合并为一行

[英]Combining similar query results into one row

I have 我有

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

and trying to make this 并试图做到这一点

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

In other words I want to combine the grop idetms into one string like here "baysboll, football" 换句话说,我想将意识形态组合成一个字符串,例如“ baysboll,football”

您需要GROUP_CONCAT函数

 select username, GROUP_CONCAT(group) FROM table GROUP BY Username

don't forget to escape the column Group with backtick in order to avoid syntax error. 不要忘记使用反引号对“ Group进行转义以避免语法错误。 GROUP is a Reserved Keyword . GROUP保留关键字

SELECT username, GROUP_CONCAT(`group`) 
FROM table
GROUP BY userName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM