Oracle中的Rownum 字段

选择表中的某一行记录:(理解:rownumoracle系统顺序分配为从查询返回的行的编号)


select * from (select rownum a,t.* from testtab t) where a=2;
select * from (select rownum a,t.* from testtab t) where a=3;
select * from (select rownum a,t.* from testtab t) where a=4;
不能为:
select * from (select rownum,t.* from testtab t) where rownum=2;

select * from testtab where rownum=2;
返回多行记录:
select * from testtab where rownum<=10;
返回某段记录:(如取记录表中4-10)
select * from (select rownum no,testtab.* from testtab where rownum<=10) where no>=4;
返回有条件且经过排序的某段记录:
select rownum num1,tt.* from
(select rownum num,t.* from
(select EcodeInfo.* from EcodeInfo where a=1 order by ecode desc) t) tt
where num>19 and rownum<20
以为oracle是先提取记录再排序的,而oraclerownum是在提取记录就已经生成,它先于排序操作,所以必须使用子查询先排序。不能为:
select * from tsettab where rownum>10;
返回最后一行记录:
select * from (select rownum a,t.* from testtab t) where a=(select count(*) from testtab);
返回最后N行记录:
select * from (select rownum a,t.* from testtab t) where a=(select count(*)-N from testtab);

SQL> select * from t1 where rownum =2;未选定行
SQL> select * from t1 where rownum =1;
SERIAL1 P
-------------------- -
1
rownum =1以外所有的 =2 =3都提示未选定行,why ?

rownum是給查詢出來的值給一個行號,沒有1就不會有2,3及以後的

是啊,看样子很多人还是不理解,老有人说早知道吃第三个饼会饱的话,就不该吃第一、二个了。

请使用浏览器的分享功能分享到微信等