有三种方法可以实现:
一、搜索前20条记录,指定不包括前10条
语句:
select top 20 * from tbl where id not in (select top 10 id from tbl)
二、搜索记录生成临时表,建立临时表的自增id。通过取得自增id的10<id<20的记录的方法取得所需数据
语句:
select identity(int,1,1) as id,* into #temp from tbl;
select * from #temp where id between 10 and 20
第二个方法实际上是两条语句,但你可以让他连续执行,就像一条语句一样完成任务。
三、朋友们觉得第一种方法效率太低,经过讨论,得出第三种方法:
语句:
SELECT TOP 10 * FROM (SELECT TOP 20 * FROM tblORDER BY id) as tbl2 ORDER BY tbl2.id DESC
(感谢iRobot提供方案
ORACLE 的写法
(select * from t_cprt_inventory_report where rownum<=20 )
MINUS
(select * from t_cprt_inventory_report where rownum<10 ) ;
rownum 使用
查出1-5的数据
select rownum,empno,ename,job,sal from emp where rownum<=5;
要查5-10的数据 得用到子查询
select * from (select rownum rn,empno,ename,job,sal from emp where rownum<=10) temp where temp.rn>5;
要查最后4条数据 也得用子查询
select * from (select rownum rn,empno,ename,job,sal from emp where rownum<=14) temp where temp.rn>10;