【问题标题】:How to limit and show results based on categories?如何根据类别限制和显示结果?
【发布时间】:2017-10-29 03:32:13
【问题描述】:

我正在尝试弄清楚如何在每个类别中显示一定数量的产品并按销售数量进行过滤。我好像搞不懂。

假设我的数据库“mysql”中有 100 个产品,它们属于 5 个不同的类别,我想每页显示 10 个结果,但每页每个类别显示 2 个结果,并按 number_of_sales 从高到低过滤。解决这个问题的最佳方法是什么?

我已经创建了一个测试表,其中有 6 列,分别是 id、product_id、product_name、category、category_id、number_of_sales

id, product_id,  product_name,  category,  category_id, number_of_sales
1  | 1          | product1    | category1 | 1          | 5
2  | 2          | product2    | category1 | 1          | 10
3  | 3          | product3    | category1 | 1          | 15
4  | 4          | product4    | category1 | 1          | 4
5  | 5          | product5    | category1 | 1          | 6
6  | 6          | product6    | category2 | 2          | 14
7  | 7          | product7    | category2 | 2          | 6
8  | 8          | product8    | category2 | 2          | 1
9  | 9          | product9    | category2 | 2          | 0
10 | 10         | product10   | category2 | 2          | 2
11 | 11         | product11   | category3 | 3          | 17
12 | 12         | product12   | category3 | 3          | 2
13 | 13         | product13   | category3 | 3          | 6
14 | 14         | product14   | category3 | 3          | 4
15 | 15         | product15   | category3 | 3          | 7
16 | 16         | product16   | category4 | 4          | 3
17 | 17         | product17   | category4 | 4          | 8
18 | 18         | product18   | category4 | 4          | 7
19 | 19         | product19   | category4 | 4          | 1
20 | 20         | product20   | category4 | 4          | 0 
21 | 21         | product21   | category5 | 5          | 6
22 | 22         | product22   | category5 | 5          | 4
23 | 23         | product23   | category5 | 5          | 7
24 | 24         | product24   | category5 | 5          | 8
25 | 25         | product25   | category5 | 5          | 5

我在 while 循环中尝试了许多不同的查询,但未能获得正确的输出。我能得到的最好结果是显示 5 个结果,使用 group by 和 order by 从最高或最低销售额的每个类别中显示 1 个。

编辑:

输出样本。有点像这样。

1   product11   | category3 | 3          | 17
2   product3    | category1 | 1          | 15
3   product6    | category2 | 2          | 14
4   product2    | category1 | 1          | 10
5   product17   | category4 | 4          | 8
6   product15   | category3 | 3          | 7
7   product18   | category4 | 4          | 7
8   product23   | category5 | 5          | 7
9   product7    | category2 | 2          | 6
10  product21   | category5 | 5          | 6

【问题讨论】:

  • 如果您像示例表一样发布预期的输出示例会更清楚。
  • 这种类型的查询在 MySQL 中很难做到。
  • 我已经看过那个页面和其他一些页面,我希望不必使用一堆工会。我已经尝试过了,但没有得到我想要的东西,但我并没有搞砸它,但我会再试一次。
  • @rich 您在下面有一个有效的答案。

标签: php mysql sql


【解决方案1】:

这个sqlfiddle怎么样

select product_name, `category`, sales
from 
(
   select product_name, `category`, sales,
      (@num:=if(@category = `category`, @num +1, if(@category := `category`, 1, 1))) row_number 
  from my_table t
  CROSS JOIN (select @num:=0, @group:=null) c
  order by `category`, sales desc, product_name
) as x 
where x.row_number <= 2 order by sales desc;

引用自this link

您可以在“sales desc”之后添加LIMIT 0, 10,其中 0 是您的偏移量,offset = (page-1)*10

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多