mysql - Phpmyadmin-每四个类别行后显示一个特定类别

标签 mysql phpmyadmin

我有一个包含四列 ID、姓名、性别、类别 ID 和 100 条记录的表。 enter image description here enter image description here

在categoryID列中,我的categoryID范围为1到5,现在我想在每四行之后显示类别1。 例如,如果我的第一行有 CategoryId 1,那么在 4 行之后,第五行应该有 CategoryId 1,然后四行有不同的类别,之后的行应该有 CategoryId 1

您可以引用上面所附的图片。

我希望结果是这样的 enter image description here enter image description here

感谢您的帮助!!!

最佳答案

我使用增量变量为 categoryID=1 和其他类别分配行号。然后我按照排名和类别 ID 对它们进行排序。请参阅下面的查询。

select 
    ID, Name, Gender, CategoryID
from  
    (select 
         ID, Name, Gender, CategoryID,
         @rnk:=@rnk+5 as rnk
     from 
         table1, (select @rnk:=-4) r
     where 
         categoryID = 1
     union all
     select 
         ID, Name, Gender, CategoryID,
         case when mod(@rnk2,5)=0 then @rnk2:=@rnk2+2
              else @rnk2:=@rnk2+1 end as rnk
     from 
         table1, (select @rnk2:=0) r
     where 
         categoryID <> 1) t
order by 
    rnk, categoryID

在此处查看演示:http://sqlfiddle.com/#!9/7a0d8/1

关于mysql - Phpmyadmin-每四个类别行后显示一个特定类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49476298/

相关文章:

mysql - sphinx 配置: Adding manual column what is not found on MySQL Database

java转mysql jdbc连接问题

php - 以编程方式从 MySQL 中选择数据

php - 使用 JOINS 连接多个 MySQL 表中的用户数据

php - 无法使用表单在数据库中插入值

php - mysql 查询在 php 中按日期条件过滤特定行?

mysql - mysql中的错误和警告有什么区别

mysql - 如何在 phpmyadmin 上制作复合主键

php - MySQL - 无法添加外键约束 - 拉丁文/西里尔文

mysql - 在 phpMyAdmin 中显示 HTML