接收组列表我有数据的表像在MySQL
UserID Item
1 apple
1 banana
2 apple
3 grapefruit
,并希望像
1 apple, banana
2 apple
3 grapefruit
结果列表。
我不知道如何寻找这一点,因为我缺乏适当的翻译:(
解决方案或大为赞赏searchterms,
接收组列表我有数据的表像在MySQL
UserID Item
1 apple
1 banana
2 apple
3 grapefruit
,并希望像
1 apple, banana
2 apple
3 grapefruit
结果列表。
我不知道如何寻找这一点,因为我缺乏适当的翻译:(
解决方案或大为赞赏searchterms,
SELECT UserID, GROUP_CONCAT(Item)
FROM `table`
GROUP BY UserID
This page of the MySQL Reference Manual further explains the functionality of GROUP_CONCAT
你是如何得出这个解决方案的? – Teja 2012-03-19 19:34:44
select userid, group_concat(item SEPARATOR ',')
from your_table
group by userid
见SqlFiddle示例
实际上有一个规范e在逗号后面:)
select UserID, group_concat(Item order by Item separator ', ') from t
group by UserID
您可以更改group_concat的排序标准。在这种情况下,我按Item按字母顺序排序,因为它似乎是您应用于结果的顺序。
检查official documentation以了解如何使用GROUP_CONCAT。
组织你的数据... – Teja 2012-03-19 19:32:14
对不起,但这并不是你想要做的事。你可以编辑这个,并提供表的模式,然后把这些记录像csv格式这样或什么? – 2012-03-19 19:33:21