我有两个包含以下信息的表
models
id manufactor
1 Mercedes
2 Ford
3 Volvo
orders
car_id customer_id
1 4
1 22
2 54
1 95
我想要一个查询,它将在一行中提供制造商名称以及每种模型的销售数量。
以下是我的尝试:
SELECT
m.make
FROM models m
LEFT JOIN(
SELECT COUNT(*)FROM orders
) o on o.car_id = m.id
答案 0 :(得分:2)
试试这个:
SELECT m.manufactor, COUNT(o.car_id)
FROM models m
LEFT JOIN orders o ON m.id = o.car_id
GROUP BY o.car_id
答案 1 :(得分:0)
SELECT m.make, sum(o.car_id) as total_sold FROM models as m
LEFT JOIN SELECT * from orders as o on o.car_id=m.id group
by o.car_id order by o.car_id
答案 2 :(得分:0)
您可以按如下方式更改查询
SELECT
m.make,o.totcount
FROM models m
LEFT JOIN(
SELECT COUNT(*) as totcount FROM orders group by car_id
) o on m.id = o.car_id