
id连续的,因此三表关联查询
select distinct(l1.num) as ConsecutiveNums
from Logs l1,Logs l2,Logs l3
where
l1.id=l2.id-1 and l1.id=l3.id-2
and l1.num=l2.num and l1.num =l3.num
方法二,利用自定义变量进行分组 ,将连续的值都分到一个组里面,然后就可以group by 分组找出来了 。
select distinct(num) as ConsecutiveNums from
(
select id,num,
if(@lastNum!=num,@group:=@group+1,@group) as g,
if(@lastNum!=num,@lastNum:=num,@lastNum) as c from
(select id,num from Logs )a,
(select @lastNum:=0,@group:=0 )b
) m group by g having count(1)>=3 order by id
本文介绍两种使用SQL查询连续重复数值的方法。一种是通过三表关联查询找出连续重复的数值,另一种是利用自定义变量进行分组,将连续的值分到同一组,通过group by分组找出连续重复超过三次的数值。

被折叠的 条评论
为什么被折叠?



