【发布时间】:2018-03-02 13:29:48
【问题描述】:
没有子查询可以只左连接一行吗?
我需要获取产品统计信息,并且某些产品有多个组。 因此,产品数量不正确。
SELECT COUNT(p.id) AS total_product, SUM(p.price) AS total_price
FROM product p
LEFT JOIN attribute_group a ON
a.product_id = p.id
WHERE p.created_at >= "2018-01-01" AND (a.id = 1 OR a.id = 2)
GROUP BY p.id
LIMIT 0, 30;
product
id | price
1 | 100
2 | 150
3 | 250
attribute_group
id | product_id | title
1 | 1 | a1
2 | 1 | a2
3 | 2 | a3
4 | 3 | a4
应该是:
1| 100
但我明白了:
2 | 200
【问题讨论】:
-
只需在没有 GROUP BY 的情况下执行 LEFT JOIN,您就会明白会发生什么。
-
解释你的逻辑。你的问题并没有解释你想要做什么,只是什么不起作用。
-
Gordon Linoff> 我需要获取按价格和属性选择的产品总价。