码迷,mamicode.com
首页 > 数据库 > 详细

MySQL 获取连续范围

时间:2015-04-21 16:06:56      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:mysql   连续范围   select   有序数字   

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连续范围问题

MySQL 获取连续范围

标签:mysql   连续范围   select   有序数字   

原文地址:http://blog.csdn.net/jrainbow/article/details/45171533

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!