-1

这是我的查询:

SELECT DISTINCT  star_user.name_user, star_group.name_group,   star_addresses.email 
FROM star_addresses

JOIN star_user_group
ON star_user_group.id_user_groupu = star_addresses.user_id

JOIN star_group
ON star_group.id_group = star_user_group.id_group_groupu

JOIN star_user
ON star_user.corespondence_user = star_addresses.id_address

JOIN star_news_group
ON star_news_group.groupid_newsg = star_user_group.id_group_groupu

WHERE newsid_newsg = 5;

运行上述查询后,我得到以下输出:

name_user|name_group|email
Zuma     |A         |abc@zz.com
Rafe     |B         |xyz@nn.com
Adam     |D         |aaa@bb.com
Zuma     |B         |abc@zz.com
Zuma     |C         |abc@zz.com

我希望拥有的不是 Zuma 的重复值,例如她的 name_user 和电子邮件:

name_user|name_group|email
Zuma     |A,B,C     |abc@zz.com

那么,如何获得呢?非常感谢您的帮助!

4

1 回答 1

0

在 MySQL 中,使用GROUP_CONCAT

SELECT  star_user.name_user, 
        GROUP_CONCAT(DISTINCT star_group.name_group) name_group,   
        GROUP_CONCAT(DISTINCT star_addresses.email) email
FROM    star_addresses
        JOIN star_user_group
            ON star_user_group.id_user_groupu = star_addresses.user_id
        JOIN star_group
            ON star_group.id_group = star_user_group.id_group_groupu
        JOIN star_user
            ON star_user.corespondence_user = star_addresses.id_address
        JOIN star_news_group
            ON star_news_group.groupid_newsg = star_user_group.id_group_groupu
WHERE   newsid_newsg = 5;
GROUP   BY star_user.name_user
于 2013-03-06T10:51:58.823 回答