-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmysqlquery.sql
More file actions
52 lines (41 loc) · 1.46 KB
/
mysqlquery.sql
File metadata and controls
52 lines (41 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use test;
select l.id + 1 as start, min(fr.id) - 1 as stop
from testa as l
left outer join testa as r on l.id = r.id - 1
left outer join testa as fr on l.id < fr.id
where r.id is null and fr.id is not null
group by l.id, r.id;
select l.id + 1 as start
from testa as l
left outer join testa as r on l.id + 1 = r.id
where r.id is null;
select start, stop from (
select m.id + 1 as start,
(select min(id) - 1 from testa as x where x.id > m.id) as stop
from testa as m
left outer join testa as r on m.id = r.id - 1
where r.id is null
) as x
where stop is not null;
SELECT (t1.id + 1) as gap_starts_at
FROM testa t1
WHERE NOT EXISTS (SELECT t2.id FROM testa t2 WHERE t2.id = t1.id + 1);
SELECT (t1.id + 1) as gap_starts_at, (SELECT MIN(t3.id) -1 FROM testa t3 WHERE t3.id > t1.id) as gap_ends_at
FROM testa t1
WHERE NOT EXISTS (SELECT t2.id FROM testa t2 WHERE t2.id = t1.id + 1)
HAVING gap_ends_at IS NOT NULL;
insert into sequence(id) values
('a'), ('b'), ('c'), ('e'),
('f'), ('g'), ('l'), ('m'), ('n');
select start, stop from (
select char(ascii(m.id) + 1) as start,
(select char(min(ascii(id)) - 1) from sequence as x where x.id > m.id) as stop
from sequence as m
left outer join sequence as r on ascii(m.id) = ascii(r.id) - 1
where r.id is null
) as x
where stop <> '';
select id, count(*) from sequence
group by id
having count(*) > 1;
https://www.xaprb.com/blog/2005/12/06/find-missing-numbers-in-a-sequence-with-sql/