[Solved] How I solve this questions?


You have to use GROUP BY command :

SELECT 
  id_unit, COUNT(*)
FROM 
  order_amount 
GROUP BY 
  id_unit;

Results :

id_unit COUNT(*)
kg  40
m3  30
pal 30

After if you want to display for each order your script will be :

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;

Results :

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

For more detail SQL Fiddle

0

solved How I solve this questions?