我有一个包含四列 ID、姓名、性别、类别 ID 和 100 条记录的表。 enter image description here
在categoryID列中,我的categoryID范围为1到5,现在我想在每四行之后显示类别1。 例如,如果我的第一行有 CategoryId 1,那么在 4 行之后,第五行应该有 CategoryId 1,然后四行有不同的类别,之后的行应该有 CategoryId 1
您可以引用上面所附的图片。
我希望结果是这样的 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/