1

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?

4
  • 3
    Just join all 3 tables, add a WHERE clause for the date, and use SUM() and GROUP BY item.id. Commented Dec 16, 2016 at 21:31
  • Can you post what you have tried? Commented Dec 16, 2016 at 21:35
  • @Barmar this worked for me. THANKS A LOT! The final query was something link this 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; Commented Dec 17, 2016 at 0:04
  • @PMateus Post it as an answer if it's different from the other answers that were already posted. Otherwise, accept one of the answers. Commented Dec 17, 2016 at 0:15

4 Answers 4

1

This is quick answer that shall do what i understood from the question, havent tried it but iam pretty sure that it will work , just check the field names

Select sum(order_items.amount),item.name
From order_items
Inner join item
On order_items.id_item = item.id
Inner join order
On order.id = order_items.id_order
Where order.when = yourdate
Group by item.name
Sign up to request clarification or add additional context in comments.

Comments

1
select sum(order_items.amount),item.name
from `order_items`
inner join `item`
on order_items.id_item = item.id
inner join `order`
on order.id = order_items.id_order
where order.when between 'start_date' and 'end_date'
group by item.name

Join three tables. Group by item name. Give your start date and end date.

Comments

1

Here you go:

SELECT SUM(order_items.amount) AS "count", item.NAME
FROM item, `order`, order_item
WHERE order_item.ID_item = item.ID
AND `order`.ID = order_item.ID_order
AND (`order`.when BETWEEN 'start_date' AND 'end_date')
GROUP BY item.ID;

Comments

1

I could get this working with this query:

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.