答案 0 :(得分:0)
您必须使用GROUP BY命令:
SELECT
id_unit, COUNT(*)
FROM
order_amount
GROUP BY
id_unit;
结果:
id_unit COUNT(*)
kg 40
m3 30
pal 30
如果您想为每个订单显示您的脚本:
SELECT
m.id_order,
(select count(*) from order_amount m2 where m2.id_order = m.id_order and m2.id_unit = 'kg') as num_kg,
(select count(*) from order_amount m2 where m2.id_order = m.id_order and m2.id_unit = 'pal') as num_pal,
(select count(*) from order_amount m2 where m2.id_order = m.id_order and m2.id_unit = 'pal') as num_m3,
COUNT(*) as total
FROM
order_amount m
GROUP BY
id_order;
结果:
id_order num_kg num_pal num_m3 total
1 5 2 2 8
2 0 2 2 4
3 6 5 5 14
4 5 0 0 10
5 5 3 3 15
详细信息SQL Fiddle