views:

510

answers:

3

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 MemberName?

+1  A: 
SELECT  MemberId, a.MemberName, GROUP_CONCAT(DISTINCT FruitName)
FROM    a
LEFT JOIN
        b
ON      a.MemberName = b.MemberName
GROUP BY
        a.MemberName
Quassnoi
+3  A: 

Add the keyword DISTINCT to the grouped column:

GROUP_CONCAT(DISTINCT FruitName)
soulmerge
EXCELLENT. THANKS EVERYONE!
Adam
+2  A: 

try

GROUP_CONCAT(Distinct FruitName)
Stan R.