我有三张桌子:
区域表:
+--------------------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| location_id | bigint(20) unsigned | NO | MUL | NULL | |
| impressions_count | bigint(20) unsigned | YES | | 0 | |
| created | datetime | YES | | NULL | |
| modified | datetime | YES | | NULL | |
+--------------------------+---------------------+------+-----+---------+----------------+
地点表:
+----------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| retailer_id | bigint(20) unsigned | NO | MUL | NULL | |
| zones_count | int(10) unsigned | YES | | 0 | |
| contacts_count | int(10) unsigned | YES | | 0 | |
| created | datetime | YES | | NULL | |
| modified | datetime | YES | | NULL | |
+----------------+---------------------+------+-----+---------+----------------+
零售商表:
+---------------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| account_rep_id | bigint(20) unsigned | YES | MUL | NULL | |
| leadsource_id | bigint(20) unsigned | YES | MUL | NULL | |
| industry_id | bigint(20) | NO | MUL | NULL | |
| name | varchar(100) | NO | | NULL | |
| locations_count | int(10) unsigned | YES | | 0 | |
| created | datetime | YES | | NULL | |
| modified | datetime | YES | | NULL | |
+---------------------+---------------------+------+-----+---------+----------------+
我想做什么:
我正在从Retailers
表中选择所有记录。 Retailers
表在Locations
中有许多记录,而这些记录在Zones
中有很多记录。
Zones
表有一个名为impressions_count
的字段。我想要做的是,在加入Retailers
表和Locations
表时,从Zones
表中选择所有记录。
基本上,我认为我需要为Locations
中的每个匹配记录返回一个SUM(Zones.impressions_count),然后为Retailers
中的每个记录返回一个总和。我一直在靠墙撞墙 - 并且感谢任何指导!
最终:我的结果集应如下所示:
**from retailers**
id,
account_rep_id,
lead_source_id,
industry_id,
impressions_count, // <- sum of related records in `Zones`
答案 0 :(得分:2)
当您加入到位置时,您可能希望使用内联视图在加入之前按位置聚合数据......所以......
这样做是因为表之间的一对多关系不会通过Location_ID人为地增加impression_count的总和
Select *
FROM retailers R
INNER JOIN Locations L
on R.ID = L.retailer_id
INNER JOIN
(Select sum(impressions_count) as cnt, Location_ID from Zones group by Location_ID) as Z
on Z.Location_ID = L.ID
现在..如果你没有在你的输出中显示位置而你想要零售商的总和..那么我们需要再次总结..这次是零售商对每个地点的价值总结......
Select R.id
,R.account_rep_id
,R.lead_source_id
,R.industry_id
,coalesce(sum(cnt),0) as impressions_count
FROM retailers R
LEFT JOIN Locations L
on R.ID = L.retailer_id
LEFT JOIN
(Select coalesce(sum(impressions_count),0) as cnt, Location_ID from Zones group by Location_ID) as Z
on Z.Location_ID = L.ID
GROUP BY R.id
,R.account_rep_id
,R.lead_source_id
,R.industry_id