简体   繁体   中英

How to SELECT on the basis of date from different table

I have two tables - sales and sale_items

sales has - id and date

sale_items has - sale_id, product_id, product name and quantity

now, when I do a sale, a record enters in 'sales' and 'sale_items'. In sale_items, in the sale_id column, it mentions the id of the sale in which it was put.

I constructed the query by which I can get the quantity of the products sold from the starting of time.

What i want to get is, the quantity of a product sold on a particular date. How do I do that?

Query I am using for getting the total quantity :

SELECT si.product_id, SUM( si.quantity ) soldQty 
from sale_items si group by si.product_id

Just join your sales table and add a where clause

SELECT si.product_id,
SUM( si.quantity ) soldQty 
FROM sale_items si
JOIN sales s ON(s.id=si.sale_id)
WHERE s.date='2014-04-30'
GROUP BY si.product_id

You can try as below by joining the tables.

select
si.product_id,
SUM( si.quantity ) soldQty 
from 
sale_items si
inner join sales s on s.id = si.sale_id
where s.date = 'your date'
group by si.product_id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM