如果您的第一个值id = 1,则所有答案都可以正常工作,否则将不会检测到该差距。例如,如果您的表ID值为3、4、5,则查询将返回6。我做了这样的事情SELECT MIN(ID+1) FROM ( SELECT 0 AS ID UNION ALL SELECT MIN(ID + 1) FROM TableX) AS T1WHERE ID+1 NOT IN (SELECT ID FROM TableX)
我想到的第一件事。不确定完全采用这种方式是否是个好主意,但应该可以。假设表为t,列为c:SELECT t1.c+1 AS gap FROM t as t1 LEFT OUTER JOIN t as t2 ON (t1.c+1=t2.c) WHERE t2.c IS NULL ORDER BY gap ASC LIMIT 1编辑:这可能是一个更快的滴答声(并且更短!):SELECT min(t1.c)+1 AS gap FROM t as t1 LEFT OUTER JOIN t as t2 ON (t1.c+1=t2.c) WHERE t2.c IS NULL