请问如何实现只写一句SQL语句,查出表A中的第501至600条记录,(注:主键是ID,且ID可能不连续)

解决方案 »

  1.   

    --参考:
    假设表中有唯一ID字段:--第一种方法
    --第11条到第30条,共选出20条记录
    select *
    from (select top 20 * from (select top 30 * from 表名 order by ID) t1 order by ID desc) t2
    order by ID
    --第二种方法
    --第11条到第30条,共选出20条记录
    select top 20 *
    from 表名
    where ID>(select max(ID) from (select top 10 ID from 表名 order by ID) t1)
    order by ID
      

  2.   

    1 select top 100 from 表名 order by id DESC--倒序下取前100即可2 t1,t2分别是他们前面的结果集的别名,这时sql的格式问题,名字随便起,叫什么都行,只要别重复。
      

  3.   

    select top 101 ID from A where ID not in
    (
        select top 500 ID from A
    )
      

  4.   

    早晨刚上班还没睡醒我刚才的sql写错了
    ,应该是:
    select top 100 * 
    from 表名 
    where id not in(select top 500 id from 表名)
    order by id DESC
      

  5.   

    select top 99 * from test t where t.id not in
     (select top 501 id from test)
      

  6.   

    select *
    from (select top 20 * from (select top 30 * from 表名 order by ID desc) t1 order by ID desc) t2
    order by ID
      

  7.   

    刚才不对,以下可以:
    select *
    from (select top 20 * from (select top 30 * from 表名 order by ID) t1 order by ID desc) t2
    order by ID