Oracle SQL實現分頁查詢

2021-07-08 14:03:15 字數 4322 閱讀 1149

sql> desc test; 

名稱 是否為空? 型別

--------------------- -------- ---------------

id not null number(8)

username varchar2(32)

password varchar2(32)

age number(3)

birthday date

address

varchar2(40)

sql> select count(*) from test;

count(*)

----------

5000001

sql> select

*from

test

where

rownum

<10;

idusername

password

agebirthday

address--

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

325610

user325610

password325610

1015

-10月-15

the_earth

325611

user325611

password325611

1115

-10月-15

the_earth

325612

user325612

password325612

1215

-10月-15

the_earth

325613

user325613

password325613

1315

-10月-15

the_earth

325614

user325614

password325614

1415

-10月-15

the_earth

325615

user325615

password325615

1515

-10月-15

the_earth

325616

user325616

password325616

1615

-10月-15

the_earth

325617

user325617

password325617

1715

-10月-15

the_earth

325618

user325618

password325618

1815

-10月-15

the_earth

這種方式比較好理解,而且內層查詢效率高,整體查詢效率較穩定。

sql> select *

2from (select row_.*, rownum rownum_

3from (select *

4from test

5order

by id asc) row_

6where rownum <= 20)

7where rownum_ >= 10;

或者

sql>  select *

2from (select test.*, rownum rownum_ from test where rownum <= 5000000

order

by id asc)

3where rownum_ >= 4999990;

查詢結果

id

username

password

agebirthday

address--

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

---4999989

user4999989

password4999989

6915

-10月-15

the_earth

4999990

user4999990

password4999990

7015

-10月-15

the_earth

4999991

user4999991

password4999991

7115

-10月-15

the_earth

4999992

user4999992

password4999992

7215

-10月-15

the_earth

4999993

user4999993

password4999993

7315

-10月-15

the_earth

4999994

user4999994

password4999994

7415

-10月-15

the_earth

4999995

user4999995

password4999995

7515

-10月-15

the_earth

4999996

user4999996

password4999996

7615

-10月-15

the_earth

4999997

user4999997

password4999997

7715

-10月-15

the_earth

4999998

user4999998

password4999998

7815

-10月-15

the_earth

4999999

user4999999

password4999999

7915

-10月-15

the_earth

已選擇11行。

採用這種方式,查詢越靠後,效率越低,整體需要查詢兩次,而且逐條比較,不推薦使用。

sql>  select *

2from test

3where id not

in4 (select id from test

5where rownum <= 20)

6and rownum <= 10

order

by id asc;

這種是採用minus函式取差集的方式獲取,同樣查詢越靠後,效率越低

sql> select *

2from test

3where rownum <= 20

4 minus

5select * from test where rownum <= 10;

待續……

Oracle SQL實現分頁查詢

sql desc test 名稱 是否為空?型別 id not null number 8 username varchar2 32 password varchar2 32 age number 3 birthday date address varchar2 40 sql select coun...

oracle sql查詢日曆

查詢當前時間所在月份的日曆 select sum d1 星期日,sum d2 星期一,sum d3 星期二,sum d4 星期三,sum d5 星期四,sum d6 星期五,sum d7 星期六 from select decode d,1,l d1,decode d,2,l d2,decode d...

oracleSQL基本查詢

create table dept deptno number primary key,dname nvarchar2 50 log nvarchar2 50 select from dept for update create table enp empno number primary key,...