对于编号起始值是1的情况
- 判断是否存在缺失的编号
SELECT '存在缺失的编号' AS gap
FROM SeqTbl
HAVING COUNT(*) <> MAX(seq);
- 查询缺失的编号(连续的编号缺失不适用)
SELECT (seq - 1) AS gap
FROM SeqTbl
WHERE (seq - 1) NOT IN ( SELECT seq FROM SeqTbl ) and (seq - 1) <> 0;
- 查询连续编号缺失的情况(需要使用中间表,中间表有完整连续的序号)
SELECT id AS gap
FROM xuhao WHERE NOT EXISTS (SELECT seq
FROM SeqTbl
WHERE SeqTbl.seq = xuhao.id)
and xuhao.id <= (SELECT MAX(seq) FROM SeqTbl);
编号起始值不是1的情况
- 判断是否存在中间缺失的编号(只关注连续性)
SELECT '存在缺失的编号' AS gap
FROM SeqTbl
HAVING COUNT(*) <> MAX(seq) - MIN(seq) + 1;
- 查询连续编号缺失的情况(需要使用中间表,中间表有完整连续的序号)
SELECT id AS gap
FROM xuhao WHERE NOT EXISTS (SELECT seq
FROM SeqTbl
WHERE SeqTbl.seq = xuhao.id)
AND xuhao.id <= (SELECT MAX(seq) FROM SeqTbl) AND
xuhao.id >= (SELECT MIN(seq) FROM SeqTbl) ;