mysql 順序列找出斷號 SQL順序列找出斷號

2021-10-18 05:42:05 字數 764 閱讀 1506

select id from info

id(11 行受影響)

方法一:

select (select max(id)+1 from info where id

from info a

where

a.id>(select max(id)+1 from info where id

beginid     endid

4           4

9           9

13         14

(3 行受影響)

方法二:

select beginid,(select min(id)-1 from info where id > beginid) as endid

from (

select id+1 as beginid from info where id+1 not in (select id from info) and id < (select max(id) from info)

) as t

beginid     endid

4           4

9           9

13          14

(3 行受影響)

說明:1、查詢結果的兩列是斷號的區間,如果beginid=endid,則表示缺少該號碼,否則表示缺少beginid ~ endid;

2、如果號碼1不存在,區間1 ~ select min(id)-1 from info 將無法找出

mysql 順序列找出斷號 SQL順序列找出斷號

select id from info id 11 行受影響 方法一 select select max id 1 from info where id from info a where a.id select max id 1 from info where id beginid endid 4...

SQL順序列找出斷號

select id from info id 12 3567 8101112 15 11 行受影響 方法一 select select max id 1 from info where id select max id 1 from info where idbeginid endid 4 4 9 ...

SQL順序列找出斷號

select id from info id 12 3567 8101112 15 11 行受影響 方法一 a.id select max id 1 from info where id beginid endid 4 4 9 9 13 14 3 行受影響 方法二 as t beginid endi...