select id from <table> where id >=11 and id<=20 and id not in(select id from myTemp) 

解决方案 »

  1.   

    to lovcal(枫兮)你的錯了
    請問樓主,還有另一個表嗎?還是隻有一個表呢。
      

  2.   

    你如果知道一個最大的id號,你用一個中間的表從最小的連續增加到最大的id,然後從這個表裡面查才行,
    從一個沒有符合條件的數據表中,是查不出任何數據的。
      

  3.   

    --创建测试环境
    create table A
    (
      ID int
    )--插入测试数据
    declare @i int
    set @i=1
    while @i<10000
    begin
         insert A select @i
         set @i=@i+2
    end--创建聚集索引
    create clustered index index_ID on A(ID)--测试
    declare @min int,   --要查询的ID最小值
            @max int,   --要查询的ID最大值
            @tmp int
    select @min=10,@max=20declare @tb table(ID int primary key)   --生成表变量(用时:0.0167)
    set @tmp=@min
    while @tmp<=@max
    begin
          insert @tb select @tmp
          set @tmp=@tmp+1
    end--查询(用时.00664+0.0376)
    select * from @tb where ID not in(select ID from A where ID between @min and @max)--结果
    /*
    ID          
    ----------- 
    10
    12
    14
    16
    18
    20(6 row(s) affected)
    */
      

  4.   

    我觉得lovcal(枫兮)的是对的,错在那里?
      

  5.   

    得先生成数据再用 lovcal(枫兮) 的方法,可以实现.
      

  6.   

    但是楼主强调了查询的效率,所以应该建一个索引,支持vivianfdlpw()