--如果id 为主键
select top 3 * from yourtable 
where id not in(select top 5 id from yourtable order by id)
order by id

解决方案 »

  1.   

    --或
    select top 3 * from where id in(
    select top 8 id from yourtable order by id)
    order by id desc
      

  2.   

    select top 4 * from 
        (select top 8 * from YouTable order by col asc) as a
    order by col desc
    --col 为随意字段
      

  3.   

    select top 3 * from where id in(
    select top 8 id from yourtable order by id)
    order by id desc
      

  4.   

    如果是查询第20000条到第20004条呢?而且要逆序.
    如果这时用select top 是不是效率会比较低?
    用游标是不是效率高一些?
      

  5.   

    如果是查询第20000条到第20004条呢?而且要逆序.
    ---那就加个自动编号:
    select id=identity(int,1,1),* into #a from yourtable order by id
    --然后就可以查询任意二个区间的数据了
    select * from #a where id>=20000 and id<=20004
      

  6.   

    而且,如果按楼上的写法取第4到第8条数据,而我恰恰只有7条数据,
    那么就会取到第3到第7条数据, 这就不对了. 嘿嘿!
    --我的第一个语句可以:测试:
    create table #a(id int,name varchar(10))
    insert #a(id,name)
    select 1,'aaa'
    union all select 2,'bbb'
    union all select 3,'bbb'
    union all select 4,'bbb'
    union all select 5,'bbb'
    union all select 6,'bbb'
    select top 3 * from #a 
    where id not in(select top 5 id from #a order by id)
    order by id
    drop table #a
    --结果:
    id          name       
    ----------- ---------- 
    6           bbb(所影响的行数为 1 行)
      

  7.   

    表里已经有了一个identity了,叫catalog_id,它是查询数据时必须保留的。
    两个identity会产生冲突。再有,被访问的数据库同一时间有多个用户在访问(用做网站数据库服务器),
    在多用户的情况下,创建临时表会不会产生冲突?
      

  8.   

    create table #a(id int identity(1,1),name varchar(10))
    insert #a
    select 'aaa'
    union all select 'bbb'
    union all select 'ccc'
    --你可以不选该自动编号列,选中也行,只要小变一下就行了:
    select no=identity(int,1,1),id=cast(id as int),name into #b from #a
    select * from #b
    --结果:
    no          id          name       
    ----------- ----------- ---------- 
    1           1           aaa
    2           2           bbb
    3           3           ccc(所影响的行数为 3 行)另:在多用户的情况下,创建临时表会不会产生冲突?
    --不会产生冲突!
      

  9.   


    declare @a table(iid int identity(1,1),dbname varchar(10))
    declare @min int,@max int 
    select @min=0,@max=10
    while @min<@max 
    begin
    set @min=@min+1
    insert @a select 'CSDN'+convert(char(4),@min)
    end
    select  * from (select  top 8 * from @a ) as a where iid  not in (select top 3 iid from @a order by iid asc) order by iid asc
      

  10.   

    使用类似
    select top 4 * from 
        (select top 8 * from YouTable order by col asc) as a
    order by col desc
    的写法,微软都推荐使用这个!
    select top效率很高,数据量大的时候只会耗用一些定位的时间,比游标效率不知道高哪里去了!
      

  11.   

    select count(*) 不就得到总行数了吗?
      

  12.   

    我那个问题,最后是这样实现的:
    select top 8 * from tablel
    where table1_id not in( 
    select top 4 table1_id from table1
    order by querytimes desc

    order by querytimes desc谢谢大家!!
      

  13.   

    select top 4 * from 
        (select top 8 * from YouTable order by col asc) as a
    order by col desc