2016-03-07 53 views
1

我有三個表:你如何獲得相關表中的字段的總數?

區表:

+--------------------------+---------------------+------+-----+---------+----------------+ 
| 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表。

從本質上講,我覺得需要在Retailers返回Locations每個匹配記錄的SUM(Zones.impressions_count),然後的一個總和爲每個記錄。我一直在頭撞牆,並感謝任何指導!

最後:我的結果集應該是這樣的:

**from retailers** 
id, 
account_rep_id, 
lead_source_id, 
industry_id, 
impressions_count, // <- sum of related records in `Zones` 

回答

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 
+0

這非常接近!唯一的問題是,它不會返回零展示次數的零售商。但是,這可能會用不同的連接修復。 –

+0

很容易將內部改爲左邊。我們可能需要合併...(更新) – xQbert

+0

像冠軍一樣工作。乾杯! –

相關問題