I tried some examples,and a lot of queries but i couldn't get this working. I have this scheme:
TABLE order
ID |...| WHEN
1 |...|2016-12-14
2 |...|2016-12-14
TABLE item
ID|...|NAME
1 |...|Pen
2 |...|Pencil
3 |...|Knife
TABLE order_itens
ID |...|amount|ID_order|ID_item|
1 |...|5 |1 |1
2 |...|2 |1 |2
3 |...|1 |1 |3
4 |...|10 |2 |1
5 |...|2 |2 |2
6 |...|5 |2 |3
ID_item is FK of TABLE.item.id; ID_order is FK of TABLE.order.id;
I need to get all registries with 'order.when<= (some custom date)', and SUM the amount of each item (Pen, Pencil, Knife, etc). Like, in december month (2016-12-01 to 2016-12-31), i have in the database this registries:
count | NAME
15 | Pen
4 | Pencil
6 | Knife
How can i accomplished this?
WHEREclause for the date, and useSUM()andGROUP BY item.id.SELECT SUM(b.amount) as count,c.name FROM `order` a, `order_itens` b, `item` c WHERE a.when>= MYDATEHERE AND b.id_order=a.id AND b.id_item=c.id GROUP BY c.id;