我面试遇到这样一个题目:
我面试遇到这样一个题目:
数据库 表A id为自增
有1W条数据
其中有一条数据被删除了
现在希望能用一条SQL 查询出被删除的ID
[解决办法]
create table #t(id int);insert #t select 1unionselect 2unionselect 3unionselect 4unionselect 5unionselect 7unionselect 8select t1.id-1 as id from (select id,row_number() over (order by id ) as num from #t)t1,(select id,row_numbe() over (order by id )+1 as num from #t)t2where t1.num=t2.num and t1.id-t2.id>1drop table #t
[解决办法]
select a.l_id
from (select rownum l_id from dual connect by rownum <= 10000) a
where a.l_id not in (select id from a)
刚才把表名写错了,我觉得这样可以实现了,期待更好的办法。
[解决办法]
select b.l_id
from (select rownum l_id from all_objects where rownum <= (select 10000 + min(id) from a)) b
where b.l_id not in (select id from a);
用聚合函数取出最小的就可以了
[解决办法]