Project in SQL differences between rows

I got a table like this, and use MYSQL 5.5

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

and I want to get the numbers 3 6 9 13 15, to be more accurate every number that fulfills the condition:

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

Answers


Here you go (this assumes, that your id column really is sorted):

/*
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

Ask, if you have any questions. Should be pretty self explaining, though.


Need Your Help