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://book.2cto.com/201211/8313.html