请高手帮我写个查询语句
我的表结构
id    test
1      1 
2      2 
3      4
4      6
5      8 
6      9
7      10
8      11
9      12
怎么才能查询出 3 5 7这三个数字。
就是不在1-12 中的数据

解决方案 »

  1.   

    select id from tlb_test
      where not (id in(select distinct test  from tbl_test ))
      

  2.   

    not in 你的结果集就可以
      

  3.   

    没有ID那个字段的话就不行!
    ----------------
    废话!你查的不就是ID字段么?
      

  4.   

    set nocount on 
    declare @T Table (id varchar(10),test varchar(10))
    --插入数据
    insert into @t
    select '1','1' union
    select '2','2' union
    select '3','4' union
    select '4','6' union
    select '5','8' union
    select '6','9' union
    select '7','10' union
    select '8','11' union
    select '9','12'
    --select test from @t
    declare @intMaxID int,@i int
    declare @intIdentity table (Rid int)
    select @intMaxID=max(convert(int,test)) from @t
    set @i=1
    while @i<@intMaxID
    begin
    insert into @intIdentity
    select @i
    set @i=@i+1
    end
    --select * from @intIdentityselect Rid from @intIdentity  where Rid not in (select test from @T)
    ------------结果 
    declare @T Table (id varchar(10),test varchar(10))
    --插入数据
    insert into @t
    select '1','1' union
    select '2','2' union
    select '3','4' union
    select '4','6' union
    select '5','8' union
    select '6','9' union
    select '7','10' union
    select '8','11' union
    select '9','12'
    --select test from @t
    declare @intMaxID int,@i int
    declare @intIdentity table (Rid int)
    select @intMaxID=max(convert(int,test)) from @t
    set @i=1
    while @i<@intMaxID
    begin
    insert into @intIdentity
    select @i
    set @i=@i+1
    end
    --select * from @intIdentityselect Rid from @intIdentity  where Rid not in (select test from @T)
    ------------------结果 
    Rid         
    ----------- 
    3
    5
    7
      

  5.   

    上面贴重复了,按下面的
    set nocount on 
    declare @T Table (id varchar(10),test varchar(10))
    --插入数据
    insert into @t
    select '1','1' union
    select '2','2' union
    select '3','4' union
    select '4','6' union
    select '5','8' union
    select '6','9' union
    select '7','10' union
    select '8','11' union
    select '9','12'
    --select test from @t
    declare @intMaxID int,@i int
    declare @intIdentity table (Rid int)
    select @intMaxID=max(convert(int,test)) from @t
    set @i=1
    while @i<@intMaxID
    begin
    insert into @intIdentity
    select @i
    set @i=@i+1
    end
    --select * from @intIdentityselect Rid from @intIdentity  where Rid not in (select test from @T)------------------结果 
    Rid         
    ----------- 
    3
    5
    7
      

  6.   

    我的写法是产生了从1到你最大值的记录,当然,你可以用max+n
    不过只要你出现的最大值后面的记录没有,不管你有多少次,这点计算使用表变量是很快的.
      

  7.   

    简单地:
    select id from atable where id not in (select test from atable);
    结果为
    3
    5
    7