Oracle rownum 案例解說

來源:互聯網
上載者:User

選擇表中的某一行記錄:(理解:rownum是Oracle系統順序分配為從查詢返回的行的編號)
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是先提取記錄再排序的,而oracle的rownum是在提取記錄就已經產生,它先於排序操作,所以必須使用子查詢先排序。
不能為:
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);

----------------
 select   *   from   adminrole   where   rownum<=4
  minus  
  select   *   from   adminrole   where   rownum<2>

 select * from
  (select rownum row_id ,b.* from (select a.* from sorttable a order by sortid)b) 
 where row_id between 5 and 9;

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.