我有tbl_profilecategory
这样的表结构:
Id col1
1 50
3 30
4 40
5 50
8 80
9 90
现在我想要表中缺少的ID,所以这里的结果应该是这样的:
Id= 2,6,7
答案 0 :(得分:2)
您可以使用
SELECT t1.id - 1 AS missing_id
FROM mytable t1
LEFT JOIN mytable t2 ON t2.id = t1.id - 1
WHERE t2.id IS NULL
答案 1 :(得分:1)
此答案基于here
SELECT a.id+1 AS start, MIN(b.id) - 1 AS end
FROM testtable AS a, testtable AS b
WHERE a.id < b.id
GROUP BY a.id
HAVING start < MIN(b.id)