查询表中已删除id的方法

方法一: 创建一个满足需求的很大的View

create   view   v_number         --创建一个view,里面存放1到9999的数据,如果需要更大可以根据自己的情况修改  
  as  
  select   number   from    
  (    
  select   t1.b+t2.b*10+t3.b*100+t4.b*1000   number    
  from    
  (    
  select   0   b   union   all   select   1   b   union   all   select   2   b   union   all    
  select   3   b   union   all   select   4   b   union   all   select   5   b   union   all    
  select   6   b   union   all   select   7   b   union   all   select   8   b   union   all   select   9   b    
  )   t1,    
  (    
  select   0   b   union   all   select   1   b   union   all   select   2   b   union   all    
  select   3   b   union   all   select   4   b   union   all   select   5   b   union   all    
  select   6   b   union   all   select   7   b   union   all   select   8   b   union   all   select   9   b    
  )   t2,    
  (    
  select   0   b   union   all   select   1   b   union   all   select   2   b   union   all    
  select   3   b   union   all   select   4   b   union   all   select   5   b   union   all    
  select   6   b   union   all   select   7   b   union   all   select   8   b   union   all   select   9   b    
  )   t3,    
  (    
  select   0   b   union   all   select   1   b   union   all   select   2   b   union   all    
  select   3   b   union   all   select   4   b   union   all   select   5   b   union   all    
  select   6   b   union   all   select   7   b   union   all   select   8   b   union   all   select   9   b    
  )   t4    
  )   t5    
  where   number<>0  
   
   
  --用左外连接找到clothid中的cid不在v_number中的数  
  select   number    
  from   v_number   left   outer   join   YWXX on   number   =   YWID   
  where   YWID   is   null   and   number   <(select   max(YWID   )   from   YWXX )  
  order   by   number 

 

方法二:

创建一个临时表
  declare   @temptable   table(a   int)  
       
  declare   @char     varchar(1000),@i   int,@num   int  
  set   @num=(select   max(YWID)   from   YWXX)  
  set   @i=1  
  while   @i<=@num  
  begin  
      insert   @temptable   values   (@i)  
      set   @i=@i+1  
  end  
  select   a   from   @temptable   where   a   not   in   (select  YWID  from  YWXX)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值