select * from dfccode where value in (select top n value form dfccode)
and value not in (select top m-1 value from dfccode)

解决方案 »

  1.   

    value 只是我随便写的,value in 只能写一个字段,如果我写
    select * from dfccode where value in (select top n value,name form dfccode)
    就不行了.
      

  2.   

    设表为T,M=5,N=20;
    以下批处理语句就能得到第M到第N行的数据。
    (可以取得自然顺序的第M到第N行数据)select top 20 * into #MM
    from T
    alter table #MM
    add autoid int identity
    go
    select top 15 #MM.除autoid这外的所有列
    from #MM
    order by autoid desc
      

  3.   

    这个好比分页,但一定要注意查询的性能问题:
    法一:
    SELECT TOP 10 *
    FROM TestTable
    WHERE (ID NOT IN
              (SELECT TOP 20 id
             FROM TestTable
             ORDER BY id))
    ORDER BY ID法二:
    SELECT TOP 10 *
    FROM TestTable
    WHERE (ID >
              (SELECT MAX(id)
             FROM (SELECT TOP 20 id
                     FROM TestTable
                     ORDER BY id) AS T))  ----from 后的派生表一定要有一个别名.
    ORDER BY ID法三:应用临时表;由于需要对临时表进行插入删除效率极低,所以不推荐使用.在上面的方法中,通常法二的效率最高.因为如果用in,那么需要到结果集中去一个一个的比较.用'>'是一次性的比较.如果想将要查找的top 行设置成变量,那么必须应用动态SQL语句.下面是我自己编的针对某一个表的分页存储过程,你可以参照他来实现你要求的功能.
    CREATE procedure fy
    @rows int ,
    @pages int
    as
    declare @stra varchar(300)            --如果数据类型的长度不够,那么存储过程将不会执行.(造成空格的丢失)
    declare @strb varchar(300)
    declare @strc varchar(300)
    declare @strd varchar(300)
    set @stra=' select top  '+cast(@rows as varchar(10))+' * from  people  where [id] >  '
    set @strb=' (select max([id]) from  (select  top  '+cast((@rows*(@pages-1)) as varchar(10))+'  [id]  from people order by [id] asc) a) ' 
    set @strc='  order by  [id]  asc  '
    set @strd=' select  top  '+cast(@rows as varchar(10))+'  *  from  people '
    if @pages=1
    begin 
    exec(@strd)
    end
    else
    begin
    exec(@stra+@strb+@strc)
    end
    GO
    建议:在你的程序编完以后,一定要测试他的查询效率.