我有一个这样的表,并使用 MYSQL 5.5
id
---
2
3
6
7
8
9
13
15
16
17
18
.
.
.
我想得到 numbers 3 6 9 13 15
,更准确的每个满足条件的数字:
id[i+1] - id[i] > 1 or id[i] - id[i-1] > 1
在这里(假设您的 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
询问,如果您有任何问题。不过,应该很自我解释。