三种常用数据库(Oracle、MySQL、SQLServer)的分页之Oracle分页

php中文网
发布: 2016-06-07 15:08:31
原创
1274人浏览过

环境 Oracle 11gR2 SQLPlus 问题 Oracle 分页 解决 --创建测试表SQL create table test 2 ( 3 id number primary key, 4 name varchar2(20) not null 5 );表已创建。--创建序列SQLSQL create sequence seq_wgb_test;序列已创建。--插入数据SQL insert into t

环境

oracle 11gr2 + sqlplus

 

问题

Oracle分页

 

解决

 

 

--创建测试表

SQL> create table test
  2  (
  3     id number primary key,
  4     name varchar2(20) not null
  5  );

表已创建。

--创建序列
SQL>
SQL> create sequence seq_wgb_test;

序列已创建。

--插入数据

SQL> insert into test(id, name) values(seq_wgb_test.nextval, 'test1');

已创建 1 行。

SQL> insert into test(id, name) values(seq_wgb_test.nextval, 'test2');

已创建 1 行。

SQL>
SQL> insert into test(id, name) values(seq_wgb_test.nextval, 'test3');

已创建 1 行。

SQL>
SQL> insert into test(id, name) values(seq_wgb_test.nextval, 'test4');

已创建 1 行。

SQL>
SQL> insert into test(id, name) values(seq_wgb_test.nextval, 'test5');

已创建 1 行。

SQL>
SQL> insert into test(id, name) values(seq_wgb_test.nextval, 'test6');

已创建 1 行。

SQL>
SQL> insert into test(id, name) values(seq_wgb_test.nextval, 'test7');

已创建 1 行。

SQL>
SQL> insert into test(id, name) values(seq_wgb_test.nextval, 'test8');

已创建 1 行。

SQL>
SQL> insert into test(id, name) values(seq_wgb_test.nextval, 'test9');

已创建 1 行。

SQL>
SQL> insert into test(id, name) values(seq_wgb_test.nextval, 'test10');

已创建 1 行。

SQL> insert into test(id, name) select seq_wgb_test.nextval, name from test;

已创建10行。

SQL> insert into test(id, name) select seq_wgb_test.nextval, name from test;

已创建20行。

SQL> insert into test(id, name) select seq_wgb_test.nextval, name from test;

已创建40行。

--执行分页

SQL> select t.*
  2  from
  3  (
  4     select rownum sn,te.*
  5     from test te
  6     where rownum <= 10
  7  )t
  8  where t.sn > 0;

        SN         ID NAME
---------- ---------- --------------------
         1          2 test1
         2          3 test2
         3          4 test3
         4          5 test4
         5          6 test5
         6          7 test6
         7          8 test7
         8          9 test8
         9         10 test9
        10         11 test10

已选择10行。
登录后复制


 

 

运行效果截图

 

三种常用数据库(Oracle、MySQL、SQLServer)的分页之Oracle分页

 

小技巧

 

快速插入数据:

 

insert into test(id, name) select seq_wgb_test.nextval, name from test;

Oracle中复制数据和MySQLSQLServer不一致,这里要注意下,因为使用的自增方式不同。

 

总结语法

 

Oracle中分页是使用子查询和rownum

 

select t.*

from

(

         select rownum sn,te.*

         from tableName te

         where rownum

)t

where t.sn > num * (page - 1);

 

--num:每页显示的行数

--page:第几页

 

对应于Web程序中分页类似:

select t.*

from

(

         select rownum sn,te.*

         from tableName te

         where rownum

)t

where t.sn > num * (pageNow - 1);

 

--pageNow:当前第几页

 

--pageSize:每页显示的记录数

 

参考资料

 

http://blog.csdn.net/wentasy/article/details/8200512

 

http://blog.csdn.net/wentasy/article/details/8200561

 

 

 

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号