親愛的小伙伴們,我們今天要講解的內容是Java的分頁的相關內容,這里主要講的是如何控制數據查詢出指定記錄的信息。
至於分頁的具體方式,這里不做過多的介紹。感興趣的話可以自己研究。
一.使用數據本身的分頁方式進行相關分頁操作
1.sql server數據的方案
方案一:
-- pager the record
-- note: n代表要求查詢的第n頁的記錄,x表示每一頁有x條記錄。
select top x * from tb
where pk_col not in (select top (n-1)*x pk_col from tb)
select top x * from tb where pk_col not in (select top (n-1)*x
-- demo(示例:)查詢第31到40頁的信息
select top 10 * from persons
where username not in(select top ((3-1)*10) username from persons)
方案二:
-- pager the record
-- note: n代表要求查詢的第n頁的記錄,x表示每一頁有x條記錄。
select top x * from tb
where pk_col in (select top (n-1)*x pk_col from tb order by pk_col asc)order by pk_col desc;
-- demo(示例:)查詢第31到40頁的信息
select top 10 * from tb where pk_col in (select top (3-1)*10 pk_col from tb order by pk_col asc) order by pk_col desc;
2.Mysql數據庫的分頁方案
MySQL數據庫實現分頁比較簡單,提供了 LIMIT函數。一般只需要直接寫到sql語句后面就行了。
LIMIT子 句可以用來限制由SELECT語句返回過來的數據數量,它有一個或兩個參數,如果給出兩個參數,
第一個參數指定返回的第一行在所有數據中的位置,從0開始(注意不是1),第二個參數指定最多返回行數。
例如: select * from table WHERE … LIMIT 10; #返回前10行
select * from table WHERE … LIMIT 0,10; #返回前10行
select * from table WHERE … LIMIT 10,20; #返回第10-20行數據
--demo
查詢31到40的結果集
select * from tb limit 30,10;
3.Oracle數據的分頁方案
考慮mySql中的實現分頁,select * from 表名 limit 開始記錄數,顯示多少條;就可以實現我們的分頁效果。
但是在oracle中沒有limit關鍵字,但是有 rownum字段rownum是一個偽列,是oracle系統自動為查詢返回結果的
每行分配的編號,第一行為1,第二行為2,以此類推。。。。
例如:select * from (select rownum rn,* from tb where rn<=40) where rn>30;
--demo
查詢31到40的結果集
select * from tb (select rownum rn,* from tb where rn<=40) where rn>30;