Mysql 如何获得相关表中字段的计数和?
我有三张桌子: 分区表:Mysql 如何获得相关表中字段的计数和?,mysql,join,count,sum,Mysql,Join,Count,Sum,我有三张桌子: 分区表: +--------------------------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------------------------+---------------------+-----
+--------------------------+---------------------+------+-----+---------+----------------+
| 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 | |
+---------------------+---------------------+------+-----+---------+----------------+
位置表:
+--------------------------+---------------------+------+-----+---------+----------------+
| 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 | |
+---------------------+---------------------+------+-----+---------+----------------+
零售商表:
+--------------------------+---------------------+------+-----+---------+----------------+
| 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 | |
+---------------------+---------------------+------+-----+---------+----------------+
我想做什么:
+--------------------------+---------------------+------+-----+---------+----------------+
| 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 | |
+---------------------+---------------------+------+-----+---------+----------------+
我正在从零售商
表中选择所有记录。零售商
表在位置有许多记录
,而区域又有许多记录
Zones
表中有一个名为impressions\u count
的字段。我要做的是从零售商
表中选择所有记录,同时连接到位置
表和区域
表
基本上,我认为我需要为位置中的每个匹配记录返回一个总和(Zones.impressions\u count),然后为零售商中的每个记录返回一个总和。我已经把头撞在墙上有一段时间了,非常感谢任何指导
最终:我的结果集应该如下所示:
**from retailers**
id,
account_rep_id,
lead_source_id,
industry_id,
impressions_count, // <- sum of related records in `Zones`
**来自零售商**
身份证件
客户代表id,
线索来源id,
工业署,
impressions_count,//当您加入到位置时,您可能希望在加入之前使用内联视图按位置聚合数据。。。所以
这样做是为了表之间的一对多关系不会人为地通过位置ID增加impression\u 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
非常接近!唯一的问题是,它不会给零售商带来零印象。这可能是固定的一个不同的连接然而。足够容易改变内部向左。我们可能需要联合起来。。。(更新)像冠军一样工作。干杯