php - 如何根据类别限制和显示结果?

标签 php mysql sql

我想弄清楚如何显示每个类别的一定数量的产品并按销售数量进行过滤。我似乎无法弄清楚。

假设我的数据库“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 个结果,每个类别中有 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

最佳答案

这个呢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

关于php - 如何根据类别限制和显示结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46996596/

相关文章:

mysql - 如何根据时间戳和 bool 值查询记录?

mysql - 在 Rails 中嵌套用户特定的表单

php - mysql全文搜索问题

mysql - 是否可以从客户端将 SQL 查询结果导出到文档中

php - mysql 查询中的文本是否可能不遵守排序规则?

MySQL:使用 .group() 时包含非聚合列错误 | rails 5.2

sql - 检索从前一天下午 6 点到今天下午 6 点的数据

php - Symfony 服务中的模拟邮件程序

php - symfony web 调试工具栏

php - html_entity_decode 也会替换吗?如果不行怎么换?