經常使用分頁查詢sql面試
先介紹一個面試題,查詢表中第200-300條數據。應用既是分頁查詢,先經過子查詢對數據進行標記,如oracle經過rownum進行標記,再取一個區間的數據。sql
一,ORACLE數據庫
關鍵字 rownumoracle
規則: post
select * from (select a.*,rownum rc from 表名 where rownum<=endrow) a where a.rc>=startrowspa
或 SELECT * FROM (SELECT A.*, ROWNUM RN FROM (SELECT * FROM TABLE_NAME) A WHERE ROWNUM <= 40) WHERE RN > 20
其中的SELECT * FROM TABLE_NAME
表示沒有進行分頁時的sql查詢語句,而ROWNUM 和RN 限定了查詢數據的範圍。code
實例:blog
select * from ( select OL.*, rownum rc from OffsiteOutreachTeam_List OL where rownum <= 48 ) OL where OL.rc >= 9 排序
SELECT * FROM (SELECT A.*, ROWNUM RN FROM (SELECT * FROM OffsiteOutreachTeam_List) A WHERE ROWNUM <= 40) WHERE RN > 20 it
select * from ( select OL.*, rownum rc from OffsiteOutreachTeam_List OL) where rc >= 9 and rc <= 20
select * from ( select OL.*, rownum rc from OffsiteOutreachTeam_List OL) where rc between 10 and 20
注意:能夠經過排序,避免不一樣頁出現重複的數據。
- SELECT * FROM
- 2 (
- 3 SELECT A.*, ROWNUM RN
- 4 FROM (SELECT ID, OWNER, OBJECT_NAME FROM TEST WHERE OWNER IS NOT NULL ORDER BY OWNER, ID) A
- 5 WHERE ROWNUM <= 20
- 6 )
- 7 WHERE RN >= 11;
二,MySQL
關鍵字:limit
MySQL數據庫分頁 Select * from 表名 limit startrow,pagesize (Pagesize爲每頁顯示的記錄條數)
三,SQL Servel
關鍵字:top
3.SQL Server 2000數據庫分頁 Select top pagesize * from 表名 where 列名 not in(select top pagesize*page 列名 from 表名 order by列名) order by列名
4.SQL Server 2005數據庫分頁 Select * from (select 列名,row_number() over(order by 列名1) as 別名from 表名) as t where t.列名1>=startrow and t.列名1<=endrow