Oracle 分頁、取期間數據、TOP前幾


Oracle沒有 sqlserver的 top number 功能。只能以期間的形式實現

 

 

代碼實現分頁,參數curPage 當前頁、pageSize 每頁行數,計算出起始結束頁碼

int startPage = (curPage - 1) * pageSize + 1;
int endPage = curPage * pageSize;

如:當前第一頁,每頁10行得到   1,10

當前第二頁,每頁10行得到21,20

....

Oracle SQL寫法

取  第一條到第十條數據(索引從1開始),同等於 TOP 10

select *
from (select t.*, rownum rn from sys_table t where rownum <= endPage )
where rn >= startPage 

 

結果如下

select *
from (select t.*, rownum rn from sys_table t where rownum <= 10)
where rn >= 1

 

 

錯誤寫法

select *
from (select t.* from sys_users t where rownum <= 10)
where rownum >= 1


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM