# 序列_RowNum分页笔记

CREATE TABLE SCOTT.S1_Score
(
ID NUMBER(10),
Score NUMBER(3),
Name VARCHAR2(10)
)

CREATE TABLE SCOTT.S2_Score
(
ID NUMBER(10),
Score NUMBER(3),
Name VARCHAR2(10)
)

CREATE TABLE SCOTT.Y2_Score
(
ID NUMBER(10),
Score NUMBER(3),
Name VARCHAR2(10),
Descr      VARCHAR2(10)
)

insert into S1_Score(ID,Score,Name) values(1,80,'张三')

create sequence seq_y2score
start 1000
increment by 1

select seq_score.nextval from dual
select seq_score.currval from dual

insert into S1_Score(ID,Score,Name) values(seq_score.nextval ,80,'张三')
insert into S2_Score(ID,Score,Name) values(seq_score.nextval ,80,'张三')
insert into S2_Score(ID,Score,Name) values(seq_y2score.nextval ,80,'张三')
insert into S2_Score(ID,Score,Name) values(5 ,80,'张三')

insert into S2_Score(ID,Score,Name)
values(seq_score.nextval ,seq_y2score.nextval,'张三')

select a.empno, rowid rid,rownum from emp a
select * from emp where rowid='AAAH14AABAAAO+HAAI'

select *,rownum from emp
select empno,ename,rownum from emp where ename>='K'
select empno,ename,rownum from emp where ename>='K' order by ename

select * from emp where rownum<=3
select * from emp where ename>='K' and rownum<=3

//错误，先取一页，在一页中排序
select * from emp where ename>='K' and rownum<=3 order by ename
正确，有排序的时候，查询第一页
select a.* from (
select * from emp where ename>='K' order by ename) a
where rownum<=3

select * from emp where rownum<=6 and rownum>3//错误
select * from emp where rownum=1//正确
select * from emp where rownum=2//错误
select * from emp where rownum>3//错误

select * from
(select a.*, rownum rn from emp a where rownum<=9)
where rn>6

select * from
(select a.*, rownum rn from emp a where ename>='K' and rownum<=9)
where rn>6

//错误，先取一页，在一页中排序
select * from
(select a.*, rownum rn from emp a where ename>='K' and rownum<=9 order by ename)
where rn>6

正确，有排序的时候，查询第2,3,4页
select * from (
select a.*,rownum rn from (
select * from emp where ename>='K' order by ename) a
where rownum<=6) where rn>3
posted @ 2009-04-28 23:43  温景良(Jason)  Views(256)  Comments(0Edit  收藏  举报