The Query:
SELECT MemberId, a.MemberName, GROUP_CONCAT(FruitName) FROM a LEFT JOIN b ON
a.MemberName = b.MemberName GROUP BY a.MemberName
Table a
MemberID MemberName
-------------- ----------
1 Al
1 Al
3 A2
Table b
MemberName FruitName
--------------- --------------
Al Apple
Al Mango
A2 Cherry
Resulting Output from above query:
上述查询产生的结果:
MemberId MemberName GROUP_CONCAT(FruitName)
3 A2 Cherry
1 A1 Apple,Apple,Mango,Mango
The actual tables I am using have 10 columns apiece so just storing everything in one table is not a workaround. That said, how can I change the query to only return 'Apple,Mango'
for MemberNam
e?
我使用的实际表格各有10列,所以只将一切存储在一个表中不是一种解决方法。也就是说,如何才能将查询更改为仅返回“Apple,Mango”作为MemberName?
3 个解决方案
#1
Add the keyword DISTINCT to the grouped column:
将关键字DISTINCT添加到分组列:
GROUP_CONCAT(DISTINCT FruitName)
#2
try
GROUP_CONCAT(Distinct FruitName)
#3
SELECT MemberId, a.MemberName, GROUP_CONCAT(DISTINCT FruitName)
FROM a
LEFT JOIN
b
ON a.MemberName = b.MemberName
GROUP BY
a.MemberName
#1
Add the keyword DISTINCT to the grouped column:
将关键字DISTINCT添加到分组列:
GROUP_CONCAT(DISTINCT FruitName)
#2
try
GROUP_CONCAT(Distinct FruitName)
#3
SELECT MemberId, a.MemberName, GROUP_CONCAT(DISTINCT FruitName)
FROM a
LEFT JOIN
b
ON a.MemberName = b.MemberName
GROUP BY
a.MemberName