用row_number来查询。
具体方法如下:
以sqlserver2008R2为例。
1、创建数据表插入数据:
create table test
(id int,
name varchar(20))
insert into test values (1000,'张三')
insert into test values (1001,'李四')
insert into test values (1002,'王五')
insert into test values (1003,'赵六')
insert into test values (1004,'孙七')
insert into test values (1005,'杨八')
insert into test values (1006,'刘九')
insert into test values (1007,'李白')
insert into test values (1008,'杜甫')
insert into test values (1009,'白居易')
insert into test values (1010,'王安石')
insert into test values (1011,'范仲淹')
insert into test values (1012,'老子')
insert into test values (1013,'庄子')
insert into test values (1014,'孙子')
insert into test values (1015,'乔丹')
insert into test values (1016,'詹姆斯')
insert into test values (1017,'韦德')
insert into test values (1018,'罗德曼')
insert into test values (1019,'梅西')
insert into test values (1020,'内马尔')
2、执行语句,查询从id由小到大,然后找出第15到第20位的数据:
select t.id,t.name,t.rn from
(select test.*,row_number() over(order by id) rn from test) t
where t.rn between 15 and 20
执行结果如下:
其中红框部分即为排名信息。
select top 6 * from table where id not in (select top 14 id from table order by id)