MySQL加入两个表并将多行合并为一个

时间:2017-11-22 14:39:35

标签: mysql sql

我有两张桌子:

sms_recipients

campaign_id | message | user_id | contact_number | status
.........................................................
1            something  12334     078237812719     1
1            something  12123     071231231232     1
2            other      12124     078123123126     0
2            other      12334     078234234212     0
2            other      42124     078124124415     1

sms_campaign

campaign_id | shop_id| campaign_type
.....................................
1             1123     marketing
2             2123     awareness
3             3231     something else
4             4432     bla bla
5             5244     last

campaign_id对于sms_campaign表来说是唯一的,有多个user_idcampaign_id相关message champagne_id是相同的全部唯一的contact_number

我想将它们组合起来,以便campaign_id status = 0 campaign_id | shop_id| campaign_type | users_mobile_numbers | message .......................................................................... 1 1123 marketing something 2 2123 awareness 078123123126,078234234212 other 3 3231 something else 4 4432 078234234212 5 5244 078124124415 SELECT c.campaign_id, shop_id, campaign_type, contact_number AS users_mobile_numbers, message FROM sms_campaign c LEFT JOIN sms_recipients r ON u.campaign_id = c.campaign_id WHERE status = 0 的{​​{1}}出现在一行和一列中,如下所示:

{{1}}

到目前为止,这是我的查询:

{{1}}

2 个答案:

答案 0 :(得分:2)

LEFT JOIN中,您需要将过滤器status = 0移动到连接条件中,或者如果将过滤器保留在WHERE子句中,则需要status = 0 OR status IS NULL以避免过滤掉没有任何消息的广告系列 - 我已经完成了第一个选项。

根据评论,您需要按广告系列列GROUP数据,并将汇总函数应用于所有未分组的列,以确保每个组只有一行 - GROUP_CONCAT将连接每个GROUP中的所有文本值。我已经随意使用MIN来解析shopmessage的值,但您可能需要另行调整。 (如果需要,您也可以在GROUP CONCAT中执行DISTINCT。)

SELECT 
  c.campaign_id, 
  MIN(shop_id) AS shop_id,
  campaign_type, 
  GROUP_CONCAT(contact_number) AS users_mobile_numbers, 
  MIN(message) AS message
FROM sms_campaign c 
LEFT JOIN sms_recipients r 
  ON u.campaign_id = c.campaign_id AND status = 0
GROUP BY c.campaign_id, campaign_type;

答案 1 :(得分:2)

r.status上的条件移动到外连接的ON子句。 (由于WHERE子句中的条件要求r.status为非NULL,否则将取消LEFT JOIN的外部性,使其等同于INNER JOIN。)

添加GROUP BY子句以折叠行。

使用GROUP_CONCAT功能组合contact_number的值。出现在SELECT列表中但未包含在GROUP BY子句中的列引用也应包含在聚合表达式中。)

SELECT c.campaign_id
     , ...

     , GROUP_CONCAT(DISTINCT r.contact_number ORDER BY r.contact_number) AS `c_numbers`

     , MIN(r.message) AS `message`

 FROM campaign c 
 LEFT
 JOIN sms_recipients r 
   ON r.campaign_id = c.campaign_id 
  AND r.status = 0
GROUP BY c.campaign_id

GROUP_CONCAT聚合函数返回的值受max_group_concat_len变量的限制。较长的值将被静默截断为最大长度。