当前位置: 代码迷 >> Sql Server >> 查看范畴区间不存在的数字
  详细解决方案

查看范畴区间不存在的数字

热度:89   发布时间:2016-04-25 01:18:51.0
查看范围区间不存在的数字
要求取1至 100之间数据库中不包含的值

数据库表T结构(值为用户添加的 不断增加的)
VALUE
22
33
99
5


也就是说取1——100之间 除数据库中值以外的所有数字 请问这个要怎样编写语句

------解决方案--------------------
select number from master..spt_values
left join T on number=T.[value] 
where type='p' and number<100 and T.[value] is null
------解决方案--------------------
SQL code
select numberfrom mster..spt_values s left join t where s.number=t.value and type='p'where t.value is null
------解决方案--------------------
修改下,把最小值最大值加上。。。
SQL code
declare @i intdeclare @n intdeclare @min intdeclare @max intset @i=0        --生成数据最小值set @n=10000    --生成数据最大值set @min=5000    --查询范围最小值set @max=10000    --查询范围最大值CREATE TABLE #t(    [ID] [int] IDENTITY(1,1) NOT NULL,    [number] [int]  NULL   )CREATE TABLE #y(    [ID] [int] IDENTITY(1,1) NOT NULL,    [number] [int]  NULL   )---插入测试数据insert into #tselect 1 union allselect 2 union allselect 3 union allselect 4 while (@i<=@n)    begin    insert into #y ([number]) values(@i)    set @i=@i+1    end---查询测试结果select y.number from #y yleft join #t T on y.number=T.[number]  where  y.number>=@min and y.number<=@max   and T.[number] is nulldrop table #tdrop table #y
------解决方案--------------------
探讨
引用:

select number from master..spt_values
left join T on number=T.[value]
where type='p' and number<100 and T.[value] is null

如果范围超过 2498呢
比如 5000——10000之间 数据库中以外的所有数字
  相关解决方案