项目中SQL之间的行差异

时间:2012-06-13 12:17:52

标签: mysql rows

我有一个这样的表,并使用MYSQL 5.5

id
---
2
3
6
7
8
9
13
15
16
17
18
.
.
.

我希望获得数字3 6 9 13 15,以便更准确地满足每个数字 条件:

id[i+1] - id[i] > 1 or id[i] - id[i-1] > 1

1 个答案:

答案 0 :(得分:0)

这里你去(假设你的id列真的已经排序了):

/*
create table asdf (id int);
insert into asdf values 
(2),
(3),
(6),
(7),
(8),
(9),
(13),
(15),
(16),
(17),
(18);
*/
select
*
from
asdf a1 
where 
(select min(id) from asdf a2 where a2.id > a1.id) - id > 1
OR
id - (select max(id) from asdf a3 where a3.id < a1.id) > 1

问,如果您有任何疑问。但应该是非常自我解释。