【发布时间】:2013-02-27 05:01:03
【问题描述】:
在表格列表下方给出。
根据位置 [23,24,25,26,27] 中产品的点击次数总和,按降序排列商店 ID。
给定 product_id = 8
link_shops_locations
shop_id | location_id
---------------------
1 | 23
2 | 24
3 | 25
3 | 26
3 | 27
products_clicks
shop_id | product_id | clicks
-----------------------------
1 | 8 | 1
2 | 7 | 3
2 | 8 | 87
3 | 8 | 21
3 | 8 | 9
link_products_shops
product_id | shop_id
---------------------
7 | 1
8 | 1
8 | 1
8 | 2
8 | 2
8 | 1
7 | 3
8 | 3
这是我尝试过的,
SELECT SUM(c.clicks) as no,
s.shop_id
FROM link_products_shops l
INNER JOIN products_clicks c
ON c.product_id = l.product_id
INNER JOIN link_shops_locations s
ON s.shop_id = c.shop_id // duplicate shop_ids gives wrong SUM
WHERE s.location_id IN (23,24,25,26,27)
GROUP BY s.shop_id
ORDER BY no DESC;
我的问题是,由于link_shops_locations表有3个shop_id,所以得到的期望SUM乘以3。我该如何解决? link_shops_locations的INNER JOIN条件与此有关吗? .一点帮助将非常有用。
【问题讨论】:
标签: mysql duplicates sum inner-join