MySQL连续范围的姊妹篇
MySQL连续范围链接如下:
http://blog.itpub.net/29254281/viewspace-1385638/
实验环境准备
create table t(a int primary key);
insert into t values(1);
insert into t values(2);
insert into t values(3);
insert into t values(100);
insert into t values(101);
insert into t values(103);
insert into t values(104);
insert into t values(105);
commit;
怎么求不连续的范围呢。
也就是 4-99,102-102
怎么得到这个范围?
先求表中数据的最小值,减一之后,赋值到一个自定义变量@a.
然后使用这个变量,得到上一行的值,
假如是连续的,每行的值减去@a应该都是1.
那么不为1的,就是不连续的范围.
select rn +1 ,a -1 from
(
select a,@a rn,@a:=a from t,(select @a:=(select min(a)-1 from t )) as a
) b where a-rn !=1;

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29254281/viewspace-2133074/
,如需转载,请注明出处,否则将追究法律责任。