查询:
SELECT MemberId, a.MemberName, GROUP_CONCAT(FruitName) FROM a LEFT JOIN b ON
a.MemberName = b.MemberName GROUP BY a.MemberName
表a
MemberID MemberName
-------------- ----------
1 Al
1 Al
3 A2
表b
MemberName FruitName
--------------- --------------
Al Apple
Al Mango
A2 Cherry
以上查询产生的结果:
MemberId MemberName GROUP_CONCAT(FruitName)
3 A2 Cherry
1 A1 Apple,Apple,Mango,Mango
我使用的实际表格各有10列,所以只将一切存储在一个表中不是一种解决方法。也就是说,如何更改查询只返回'Apple,Mango'
e?
MemberNam
答案 0 :(得分:8)
将关键字DISTINCT添加到分组列:
GROUP_CONCAT(DISTINCT FruitName)
答案 1 :(得分:5)
尝试
GROUP_CONCAT(Distinct FruitName)
答案 2 :(得分:3)
SELECT MemberId, a.MemberName, GROUP_CONCAT(DISTINCT FruitName)
FROM a
LEFT JOIN
b
ON a.MemberName = b.MemberName
GROUP BY
a.MemberName