繁体   English   中英

分组时限制数量记录

[英]Limit Number records when Grouped

我想获得在特定年份的特定月份销售最多的十大产品。 我做了以下事情:

SELECT Year(date1), Month(date1), `ProductID`, sum(`Price`*`Quantity`)"Sales" 
FROM `products`
Group By Year(date1), Month(date1), ProductId
order by Year(date1), Month(date1), Sales Desc;

但是,它给了我所有产品,我只想要排名前十的产品。 不知道在哪里应用限制10条记录,

如果您按照需要设置了排序,只需添加

LIMIT 0,10

对您的查询

在选择查询的末尾使用mysql内置函数Limit

LIMIT {[offset,] row_count | row_count OFFSET offset}
            ^                    ^
        Starting tuple     Number of tuples

欲了解更多信息,请点击这里

考虑重新构造group by子句,order by,where子句和date_format的用法

SELECT date_format(date1) as month, `ProductID`, sum(`Price`*`Quantity`)"Sales" 
FROM `products`
WHERE date1 > '2013-01-01' and date1 < '2013-02-01'
Group By ProductId
order by Sales Desc
Limit 0,10
;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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