分页查询

分页查询

按雇员的id号升序取出


Oracle分页一共有 三种方式

1.rownum分页  rownum是oracle提供的一种标识行号的

//1.先子查询(select * from emp)

//2.显示rownum[oracle分配]

SQL> select a1.*,rownum rn from (select * from emp) a1;

//3.先将rownum<=10的取出来

SQL> select a1.*,rownum rn from (select * from emp) a1 where rownum<=10);

//4.再将rownum>=6的取出来

SQL> select * from (select a1.*,rownum rn from (select * from emp) a1 where rownum<=10) where rn>=6;

上面的rownum和rn都不能换,这是固定用法


几个查询变化

a.如果想要改变显示的数据,只需要改变最里面括号里的子查询(select * from emp)就可以了

如:按雇员的id号升序取出但是只想查看雇员的名字和工资(只取6-10)
SQL>select * from (select a1.*,rownum rn from (select ename,sal from emp) a1 where rownum<=10) where rn>=6;


b.如何排序,只需修改最里面括号里的子查询即可

如:按雇员的工资升序取出但是只想查看雇员的名字和工资(只取6-10)
SQL> select * from (select a1.*,rownum rn from (select ename,sal from emp order by sal) a1 where rownum<=10) where rn>=6;



?如何按雇员的工资升序取出但是只想查看雇员的名字和工资只显示第4条-第九条
SQL> select * from (select a1.*,rownum rn from (select ename,sal from emp order by sal) a1 where rownum<=9) where rn>=4;

分页查询总共有三种方式:

1.根据ROWID来分
select * from t_xiaoxi where rowid in(select rid from (select rownum rn,rid from (select rowid,ci from t_xiaoxi order by cid desc) where rownum<10000) where rn>9980) order by cid desc;
执行时间0.03秒

2.按分析函数来分
select * from (select t.*,row_number() over(order by cid desc) rk from t_xiaoxi t) where rk<10000 and rk>9980;
执行时间1.01秒

3.按ROWNUM来分
select * from (select t.*,rownum rn from (select * from x_xiaoxi order by cid desc) t where rownum<10000) where rn>9980;
执行时间0.1秒

其中t_xiaoxi为表名称,cid为表的关键字段,取按CID降序排序后的第9981-9999条记录,t_xiaoxi表有70000多条记录

效率:1最好,3次之,2最差

//测试通过的分页查询okokook
select * from (select a1.*,rownum rn from (select ename,job from emp) a1 where rownum<=10) where rn>=5;

你可能感兴趣的:(oracle)