我在Sql server
中有这个表ID | LetterID | LetterName
ID => int和identity
LetterID => int和unique和NotNull
LetterName =>串
LetterID
填写我的C#应用程序和用户集编号。例如1,2,3,4,5,...,100,..(每行增加一个单位),现在我的LetterID
是100
但有时用户从表中删除一行,例如删除行LetterID
为50
,现在插入新行(在应用程序中)我向他建议LetterID
选择50
,如何从表中获取丢失的数字?
答案 0 :(得分:1)
var output = Enumerable.Range(1, list.Max(item => item.LetterID))
.Except(list.Select(item => item.LetterID))
答案 1 :(得分:0)
select t1.ID, t1.LetterID-1
from yourtable t1
left join yourtable t2
on t1.LetterID = t2.LetterID+1
and t1.ID = t2.ID
where t2.ID is null
and t1.LetterID>(select MIN(LetterID) from yourtable where ID = t1.ID)
答案 2 :(得分:0)
;with cte as
(
select max(LetterID) as id from your_table
union all
select id-1 from cte where id>1
)
select cte.id as LetterID
from cte
left join your_table yt on cte.id=yt.LetterID
where yt.id is null
order by cte.id
答案 3 :(得分:0)
获取空白/缺失的行
SELECT TOP 1 x.LetterID +1
FROM tablename x
WHERE NOT EXISTS(SELECT * FROM tablename xx WHERE xx.LetterID = x.LetterID + 1)
ORDER BY x.LetterID