SQL在4月份找到最畅销的产品

时间:2014-03-18 14:13:30

标签: mysql sql product sales

我需要在4月份从我的数据库中找到最畅销的产品。 我有两个叫做销售和销售线的表。

销售表包括全年的每笔销售,销售线表包括每次销售中销售的每件产品。

销售表包括以下内容:

Sale_ID
Payment_ID
Ship_ID
Sales_Total
Date
Time
Cusotomer_ID

salesline表包括:

Salesline_ID
Product_ID
SalePrice
Sale_ID
Payment ID

由于

2 个答案:

答案 0 :(得分:0)

试试这个:

如果您想通过售出数量来确定:

select product_id, count(product_id)
from salesline
where sale_id in (select sale_id from sales where date BETWEEN '2013-04-01' AND '2013-04-30')
group by product_id

答案 1 :(得分:0)

如果您想根据SalePrice表的SalesLine列查找畅销产品,可以进行以下查询: -

    select sl.Product_ID,sum(sl.SalePrice) Sales
    from
    Sales s
    inner join
    salesline sl
    on s.Sale_ID = sl.Sale_ID
    where DATE_FORMAT(s.Date,'%m') = 4
    and DATE_FORMAT(s.Date,'%Y') = 2014
    group by sl.Product_ID
    order by Sales desc
    limit 10

这将为您提供2014年4月的十大销售产品。