1. 程式人生 > >資料庫分頁操作

資料庫分頁操作

1、Oracle資料庫:

A.select * from ( select a.*, rownum rn from (select * from table) a where rownum <= 40 ) where rn >= 21

B.select * from ( select a.*, rownum rn from (select * from table) a ) where rn between 21 and 40

C.select a.* from (select *,rownum rn from table where rownum <=5) a where rn >=3; 

例:select * from ( select t.*,rownum rn from ( select * from table) t where rownum<=(pageno*pagesize) )where rn>(paegno-1)*pagesize

2、MySQL資料庫

A.select * from table limit 10,20

例:select * from table limit (pageno-1)*pageSize,pageno*pageSize

3、SqlServer資料庫

A.select * from ( select *, row_number() over(order by id ) as id from table) as b where id between 10 and 20 

B.select b.* from (

select top 10 a.* from (

select top 20 * from table order by id asc ) -- 其中裡面這層,必須指定按照升序排序,省略的話,查詢出的結果是錯誤的。
as a order by a.id desc ) b order by b.id asc

C.select * from table order by id offset 4 rows fetch next 5 rows only(SQL2012以上的版本才支援:推薦使用 )

例:select b.* from (select top (pageno-1)*pageSize a.* from (select top pageno*pageSize * from table order by id asc) a order by a.id desc ) b order by b.id asc