3 回答

TA貢獻1802條經(jīng)驗 獲得超5個贊
這個問題經(jīng)常出現(xiàn),可悲的是,最常見(也是最可移植的)答案是創(chuàng)建一個臨時表來保存應該存在的ID ,然后進行左聯(lián)接。MySQL和SQL Server之間的語法非常相似。唯一真正的區(qū)別是臨時表語法。
在MySQL中:
declare @id int
declare @maxid int
set @id = 1
select @maxid = max(id) from tbl
create temporary table IDSeq
(
id int
)
while @id < @maxid
begin
insert into IDSeq values(@id)
set @id = @id + 1
end
select
s.id
from
idseq s
left join tbl t on
s.id = t.id
where t.id is null
drop table IDSeq
在SQL Server中:
declare @id int
declare @maxid int
set @id = 1
select @maxid = max(id) from tbl
create table #IDSeq
(
id int
)
while @id < @maxid --whatever you max is
begin
insert into #IDSeq values(@id)
set @id = @id + 1
end
select
s.id
from
#idseq s
left join tbl t on
s.id = t.id
where t.id is null
drop table #IDSeq

TA貢獻1808條經(jīng)驗 獲得超4個贊
我登陸此頁面希望找到SQLITE的解決方案,因為這是我在搜索相同問題的SQLITE時找到的唯一答案。
我找到的最終解決方案是來自本文的 浮動中間博客-SQLITE答案
希望它可以幫助別人:-)
簡單的解決方案是:
SELECT DISTINCT id +1
FROM mytable
WHERE id + 1 NOT IN (SELECT DISTINCT id FROM mytable);
天才。
添加回答
舉報