如何在mysql中将多行合并成一行

zwghvu4y  于 2021-06-17  发布在  Mysql
关注(0)|答案(3)|浏览(471)

我有一个连接4个表的查询。表格包括用户、用户信息、电子邮件和电话。一个用户可以有一个或多个电话和电子邮件。
以下是sql查询:

SELECT ur.login_id as loginId, ue.user_email_address as email, up.phone_number as phone
      FROM greydeltadb.gd_user ur 
      LEFT JOIN greydeltadb.gd_user_info uri ON uri.user_id=ur.user_id  
      INNER JOIN greydeltadb.gd_user_emails ue ON ur.user_id = ue.user_id  
      INNER JOIN greydeltadb.gd_phone up  ON ur.user_id = up.user_id  
      ORDER BY ur.login_id

当我执行此查询时,对于一个有2封电子邮件和2部电话的用户,我得到如下结果:

loginId                  email                        phone
abc                      abc@gmail.com                123456
abc                      abc@gmail.com                798446
abc                      def@gmail.com                123456
abc                      def@gmail.com                798446

然后我了解了groupconcat(),更新后的查询是:

SELECT ur.login_id, GROUP_CONCAT(ue.user_email_address) as emails,
      GROUP_CONCAT(up.phone_number) as phones
      FROM greydeltadb.gd_user ur 
      LEFT JOIN greydeltadb.gd_user_info uri ON uri.user_id=ur.user_id  
      INNER JOIN greydeltadb.gd_user_emails ue ON ur.user_id = ue.user_id  
      INNER JOIN greydeltadb.gd_phone up  ON ur.user_id = up.user_id  
      GROUP BY ur.login_id

此查询返回结果:

loginId                  email                                                                    phone
abc                      abc@gmail.com, def@gmail.com,abc@gmail.com, def@gmail.com                123456, 798446,123456, 798446

但我希望结果是:

loginId                  email                                        phone
abc                      abc@gmail.com, def@gmail.com                123456, 798446

请帮忙。

4si2a6ki

4si2a6ki1#

你只需要一个 distinct ```
SELECT ur.login_id,
GROUP_CONCAT(distinct ue.user_email_address) as
emails,
GROUP_CONCAT(distinct up.phone_number) as
phones
FROM greydeltadb.gd_user ur
LEFT JOIN greydeltadb.gd_user_info uri ON
uri.user_id=ur.user_id
INNER JOIN greydeltadb.gd_user_emails ue ON
ur.user_id = ue.user_id
INNER JOIN greydeltadb.gd_phone up ON ur.user_id =
up.user_id
GROUP BY ur.login_id

tzcvj98z

tzcvj98z2#

只需使用distinct子句,请参阅group\ U concat的文档:

> GROUP_CONCAT([DISTINCT] expr [,expr ...]
>              [ORDER BY {unsigned_integer | col_name | expr}
>                  [ASC | DESC] [,col_name ...]]
>              [SEPARATOR str_val])

在mysql中,可以获得表达式组合的串联值。要消除重复值,请使用distinct子句。要对结果中的值进行排序,请使用order by子句。。。。。。。。。。。。。。。。。
如果您有疑问,只需使用:

SELECT ur.login_id, 
      GROUP_CONCAT( DISTINCT ue.user_email_address ) as emails,
      GROUP_CONCAT( DISTINCT up.phone_number ) as phones
      .....
nlejzf6q

nlejzf6q3#

首先,你没有使用 uri ,因此在查询中不需要它。
第二,你有一个 left join 基本上什么也不做。我怀疑你想要的是用户,甚至是那些没有电话或电子邮件的用户。如果是:

SELECT ur.login_id,
       GROUP_CONCAT(DISTINCT ue.user_email_address) as emails,
       GROUP_CONCAT(DISTINCT up.phone_number) as phones
FROM greydeltadb.gd_user ur LEFT JOIN
     greydeltadb.gd_user_emails ue
     ON ur.user_id = ue.user_id LEFT JOIN
     greydeltadb.gd_phone up
     ON ur.user_id = up.user_id  
GROUP BY ur.login_id  ;

相关问题