【问题标题】:How do you get the count sum of a field in a related - related table?您如何获得相关表中字段的计数总和?
【发布时间】:2016-03-07 16:38:54
【问题描述】:

我有三张桌子:

区域表:

+--------------------------+---------------------+------+-----+---------+----------------+
| 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 中有很多记录。

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`

【问题讨论】:

    标签: mysql join count sum


    【解决方案1】:

    当您加入位置时,您可能希望在加入前使用内联视图按位置聚合数据...所以...

    这样做是为了表之间的一对多关系不会人为地增加位置 ID 的印象数总和

    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
    

    【讨论】:

    • 非常接近!唯一的问题是,它不会返回展示次数为零的零售商。然而,这可以通过不同的连接来解决。
    • 很容易将内部向左更改。我们可能需要合并...(更新)
    • 像冠军一样工作。干杯!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-07
    • 2023-03-10
    • 1970-01-01
    • 2021-11-06
    • 1970-01-01
    • 2018-04-01
    相关资源
    最近更新 更多