mySQL查询连接五个表

时间:2014-11-03 06:58:23

标签: mysql join

我有以下五个表(为简洁起见省略了一些字段)

(campaign)
campaign_id
===========
campaign_name
campaign_description


(ad_group)
campaign_id
===========
group_id
===========
group_name


(used_ad)
campaign_id
===========
group_id
===========
ad_id
===========


(billboard_group)
campaign_id
===========
group_id
===========
group_name


(used_billboard)
campaign_id
==============
group_id
==============
billboard_code
==============

所以"广告系列"由1)" ad_group"和2)a" billboard_group"组成。 ad_group包含许多广告(存储在" used_ad"组中),而billboard_group包含许多广告牌(存储在" used_billboard"组中)。

我想运行一个返回所有广告系列的查询,并列出与该广告系列相关的广告和广告牌总数。我只能为ad_group执行此操作,但不能执行ad_group AND billboard_group。

例如,此查询:

SELECT campaign.*, COUNT(used_ad.ad_id) AS used_ads
FROM campaign
LEFT JOIN ad_group 
  ON campaign.campaign_id = ad_group.campaign_id
LEFT JOIN used_ad
  ON ad_group.group_id = used_ad.group_id
  AND used_ad.campaign_id = campaign.campaign_id
GROUP BY campaign.campaign_id

提供这样的列表:

campaign_id     campaign_name     campaign_description    used_ads
1               Test campaign     Testing                 4
2               Second campaign   Second                  0

但我追求的是:

campaign_id     campaign_name     campaign_description    used_ads    used_billboards
1               Test campaign     Testing                 4           5
2               Second campaign   Second                  0           3

只有我无法弄清楚如何在BOTH used_ad和used_billboard中包含计数。我很感激任何帮助。

3 个答案:

答案 0 :(得分:1)

为避免笛卡尔乘法,我会分别对used_adused_billboard进行分组和计数,然后才加入它们:

SELECT campaign.*, ag.used_ads_count, bg.used_billboards_count
FROM campaign c
LEFT JOIN  (SELECT    campaign_id, COUNT(*) AS used_ads_count
            FROM      ad_group ag
            LEFT JOIN used_ad ua ON ag.group_id = ua.group_id
            GROUP BY  campaign_id) ac ON c.campaign_id = ac.campaign_id
LEFT JOIN  (SELECT    campaign_id, COUNT(*) AS used_billboards_count
            FROM      billboard_group bg
            LEFT JOIN used_billboard ub ON bg.group_id = ub.group_id
            GROUP BY  campaign_id) bc ON c.campaign_id = bc.campaign_id

答案 1 :(得分:1)

如果billboard_code是唯一的,那么像这样的东西可能会起作用:

SELECT
  c.campaign_id, c.campaign_name, 
  COUNT(DISTINCT u.ad_id) AS used_ads,
  COUNT(DISTINCT b.billboard_code) AS used_billboards
FROM
campaign c
LEFT JOIN ad_group a ON c.campaign_id = a.campaign_id
LEFT JOIN used_ad u ON a.group_id = u.group_id
  AND c.campaign_id = u.campaign_id
LEFT JOIN used_billboard b ON c.campaign_id = b.campaign_id
  AND b.group_id = a.group_id
GROUP BY c.campaign_id

答案 2 :(得分:1)

SELECT campaign.*, COUNT(used_ad.ad_id) AS used_ads, COUNT(used_billboard.billboard_code) as used_billboards
FROM campaign
LEFT JOIN ad_group 
  ON campaign.campaign_id = ad_group.campaign_id
LEFT JOIN billboard_group
  ON campaign.campaign_id = billboard_group.campaign_id
LEFT JOIN used_ad
  ON ad_group.group_id = used_ad.group_id
  AND used_ad.campaign_id = campaign.campaign_id
LEFT JOIN used_billboard
  ON billboard_group.group_id = used_billboard.group_id
  AND used_billboard.campaign_id = campaign.campaign_id
GROUP BY campaign.campaign_id