1、原始数据
2、把running_number转成数据,并加上一列有序数字
SELECT d.running_number+0 running_number,@a:=@a+1 rn FROM device_data d,(SELECT @a:=0) a
where d.device_id=13 order by d.running_number;
3、running_number与有序数据的差,差(diff)相同的的running_number说明是连续的
SELECT running_number,rn,running_number-rn diff
FROM
(SELECT d.running_number+0 running_number,@a:=@a+1 rn
FROM device_data d,(SELECT @a:=0) a where d.device_id=13) b;
4、根据差来分组,并获得相关差的最小及最大running_number,就形成了连续范围
select min(c.running_number) min,max(c.running_number) max
from
(SELECT running_number,rn,running_number-rn diff
FROM
(SELECT d.running_number+0 running_number,@a:=@a+1 rn
FROM device_data d,(SELECT @a:=0) a where d.device_id=13)
b)c group by diff;
5、参考《MySQL技术内幕:SQL编程》 2.6.2连续范围问题
原文地址:http://blog.csdn.net/jrainbow/article/details/45171533