2013-03-13 63 views
0

我想获得在特定年份的特定月份中销量最高的十大产品。我做了以下:分组时的限制数记录

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条记录申请限制,

+0

我认为你将有一个问题,这个问题,因为你是年/月/的productId上市条款和使用。一条直线的“限制”将会给你排名前10位的年/月/ productID组合,而不仅仅是前10位的产品 – 2013-03-13 19:10:21

回答

1

如果你排序是设置为你想要的,只是添加

LIMIT 0,10 

到您的查询

0

使用MySQL内置的功能限制在您选择查询

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

年底欲了解更多阅读Go Here

0

考虑,其中by子句重组组,秩序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 
;