数据库

位置:IT落伍者 >> 数据库 >> 浏览文章

使用三层嵌套正确实现Oracle分页


发布日期:2018年09月18日
 
使用三层嵌套正确实现Oracle分页

原始记录

select t* trowid from t_stu t

order by ts_birthday desc

返回

STU_ID S_NAME C_ID S_BIRTHDAY

S 李四 C

S C

S C

S C

S C

Oracle分页查询

第一种两层嵌套查询(网上流行的一种错误)

select * from

(

select rownum rt* from t_stu t

)

where r between and order by s_birthday desc

返回

R STU_ID S_NAME C_ID S_BIRTHDAY

S 李四 C

S 张三丰 C

S 张三 C

说明上面的结果可能与你的不一样因为Oracle在没有order by的情况是随机选取记录的名的记录应该是

STU_ID S_NAME C_ID S_BIRTHDAY

S 李四 C

S C

S C

第二种换用三层嵌套查询

select b* from

(select rownum ra* from

(select t* from t_stu t order by s_birthday desc )a

)b

where br between and

返回

R STU_ID S_NAME C_ID S_BIRTHDAY

S 李四 C

S C

S C

正确!

所以Oracle分布查询一定要用三层嵌套步骤如下

第三层分页过滤

select b*

from (

第二层给定行号

select rownum ra* from (

第一层排序

select * from 表 order by 字段

) a

where rownum<=最大行

)b

where br between 最小行 and 最大行

★★★关键点先排序后给行号两个步骤要分开!

为了程序的通用性对任意数据集都能分页利用子查询改为如下结构

第三层分页过滤

select b*

from (

第二层给定行号

select rownum ra* from (

第一层排序

select * from (一个已经排序的数据集)

) a

where rownum<=最大行

)b

where br between 最小行 and 最大行

如上面的查询改为

第三层分页过滤

select b*

from (

第二层给定行号

select rownum ra* from (

第一层排序

select * from (select t* from t_stu t order by s_birthday desc)

) a

where rownum<=最大行

)b

where br between 最小行 and 最大行

或者其它查询语句

第三层分页过滤

select b*

from (

第二层给定行号

select rownum ra* from (

第一层排序

select * from (select t* from 新闻表 t order by 发贴日期 desc)

) a

where rownum<=最大行

)b

where br between 最小行 and 最大行

★原始数据数据脚本(请在命令窗口中粘贴以下语句即可)

prompt PL/SQL Developer import file

prompt Created on 日 星期一 by Administrator

set feedback off

set define off

prompt Dropping T_STU

drop table T_STU cascade constraints;

prompt Creating T_STU

create table T_STU

(

STU_ID CHAR()

S_NAME CHAR()

C_ID CHAR()

S_BIRTHDAY DATE

S_SEX CHAR()

)

;

prompt Disabling triggers for T_STU

alter table T_STU disable all triggers;

prompt Loading T_STU

insert into T_STU (STU_ID S_NAME C_ID S_BIRTHDAY S_SEX)

values (S 张三 C to_date( ddmmyyyy) null);

insert into T_STU (STU_ID S_NAME C_ID S_BIRTHDAY S_SEX)

values (S 李四 C to_date( ddmmyyyy) null);

insert into T_STU (STU_ID S_NAME C_ID S_BIRTHDAY S_SEX)

values (S 张三丰 C to_date( ddmmyyyy) null);

insert into T_STU (STU_ID S_NAME C_ID S_BIRTHDAY S_SEX)

values (S C to_date( ddmmyyyy) null);

insert into T_STU (STU_ID S_NAME C_ID S_BIRTHDAY S_SEX)

values (S C to_date( ddmmyyyy) null);

insert into T_STU (STU_ID S_NAME C_ID S_BIRTHDAY S_SEX)

values (S C to_date( ddmmyyyy) null);

insert into T_STU (STU_ID S_NAME C_ID S_BIRTHDAY S_SEX)

values (S C to_date( ddmmyyyy) null);

insert into T_STU (STU_ID S_NAME C_ID S_BIRTHDAY S_SEX)

values (S C to_date( ddmmyyyy) null);

insert into T_STU (STU_ID S_NAME C_ID S_BIRTHDAY S_SEX)

values (S C to_date( ddmmyyyy) null);

insert into T_STU (STU_ID S_NAME C_ID S_BIRTHDAY S_SEX)

values (S C to_date( ddmmyyyy) null);

insert into T_STU (STU_ID S_NAME C_ID S_BIRTHDAY S_SEX)

values (S C to_date( ddmmyyyy) null);

insert into T_STU (STU_ID S_NAME C_ID S_BIRTHDAY S_SEX)

values (S C to_date( ddmmyyyy) null);

insert into T_STU (STU_ID S_NAME C_ID S_BIRTHDAY S_SEX)

values (S C to_date( ddmmyyyy) null);

insert into T_STU (STU_ID S_NAME C_ID S_BIRTHDAY S_SEX)

values (S C to_date( ddmmyyyy) null);

insert into T_STU (STU_ID S_NAME C_ID S_BIRTHDAY S_SEX)

values (S C to_date( ddmmyyyy) null);

insert into T_STU (STU_ID S_NAME C_ID S_BIRTHDAY S_SEX)

values (S C to_date( ddmmyyyy) null);

insert into T_STU (STU_ID S_NAME C_ID S_BIRTHDAY S_SEX)

values (S C to_date( ddmmyyyy) null);

insert into T_STU (STU_ID S_NAME C_ID S_BIRTHDAY S_SEX)

values (S C to_date( ddmmyyyy) null);

insert into T_STU (STU_ID S_NAME C_ID S_BIRTHDAY S_SEX)

values (S C to_date( ddmmyyyy) null);

insert into T_STU (STU_ID S_NAME C_ID S_BIRTHDAY S_SEX)

values (S C to_date( ddmmyyyy) null);

insert into T_STU (STU_ID S_NAME C_ID S_BIRTHDAY S_SEX)

values (S C to_date( ddmmyyyy) null);

insert into T_STU (STU_ID S_NAME C_ID S_BIRTHDAY S_SEX)

values (S C to_date( ddmmyyyy) null);

insert into T_STU (STU_ID S_NAME C_ID S_BIRTHDAY S_SEX)

values (S C to_date( ddmmyyyy) null);

insert into T_STU (STU_ID S_NAME C_ID S_BIRTHDAY S_SEX)

values (S C to_date( ddmmyyyy) null);

insert into T_STU (STU_ID S_NAME C_ID S_BIRTHDAY S_SEX)

values (S C to_date( ddmmyyyy) null);

insert into T_STU (STU_ID S_NAME C_ID S_BIRTHDAY S_SEX)

values (S C to_date( ddmmyyyy) null);

insert into T_STU (STU_ID S_NAME C_ID S_BIRTHDAY S_SEX)

values (S C to_date( ddmmyyyy) null);

insert into T_STU (STU_ID S_NAME C_ID S_BIRTHDAY S_SEX)

values (S C to_date( ddmmyyyy) null);

insert into T_STU (STU_ID S_NAME C_ID S_BIRTHDAY S_SEX)

values (S C to_date( ddmmyyyy) null);

insert into T_STU (STU_ID S_NAME C_ID S_BIRTHDAY S_SEX)

values (S C to_date( ddmmyyyy) null);

insert into T_STU (STU_ID S_NAME C_ID S_BIRTHDAY S_SEX)

values (S C to_date( ddmmyyyy) null);

insert into T_STU (STU_ID S_NAME C_ID S_BIRTHDAY S_SEX)

values (S C to_date( ddmmyyyy) null);

insert into T_STU (STU_ID S_NAME C_ID S_BIRTHDAY S_SEX)

values (S C to_date( ddmmyyyy) null);

insert into T_STU (STU_ID S_NAME C_ID S_BIRTHDAY S_SEX)

values (S C to_date( ddmmyyyy) null);

insert into T_STU (STU_ID S_NAME C_ID S_BIRTHDAY S_SEX)

values (S C to_date( ddmmyyyy) null);

insert into T_STU (STU_ID S_NAME C_ID S_BIRTHDAY S_SEX)

values (S C to_date( ddmmyyyy) null);

commit;

prompt records loaded

prompt Enabling triggers for T_STU

alter table T_STU enable all triggers;

set feedback on

set define on

prompt Done

上一篇:开源之旅——数据库篇(图)

下一篇:ORACLE中的两个概念:user和schema的区别和联系