昨天做完项目后让测试测试了一把,测试说分页查询貌似不起作用,翻到第4页以后,看到的数据结果都是相同的。
当时我就觉得很纳闷,不可能啊,分页组件应该是好的,咋可能有问题呢。带着疑问,我打开了自己的ide,在自己的机器上跑了一把,果然有问题。
有问题就要找问题:
首先把2条查询结果相同的sql打印出来到数据库中执行:
sql1: select * from (select t.*, rownum rn from (select t_e_id, t_e_name, t_e_tel, t_e_areacode from (select t.eid t_e_id, t.ename t_e_name, t.etel t_e_tel, t.areaid t_e_areacode, t.biz_delete_time, decode(areaid, '0730', '0', '1') orderseq from vr_enterprise t where t.eid not in (select eid from t_biz_erelation)) order by orderseq, biz_delete_time nulls last) t where rownum < 25) where rn >= 19 sql2: select * from (select t.*, rownum rn from (select t_e_id, t_e_name, t_e_tel, t_e_areacode from (select t.eid t_e_id, t.ename t_e_name, t.etel t_e_tel, t.areaid t_e_areacode, t.biz_delete_time, decode(areaid, '0730', '0', '1') orderseq from vr_enterprise t where t.eid not in (select eid from t_biz_erelation)) order by orderseq, biz_delete_time nulls last) t where rownum < 18) where rn >= 12
结果显示大多数行是相同的。
为了找到问题所在,只有先一步一步的精简化sql,看在哪一步出的问题。
于是找到了,问题出现在where rownum<18的时候数据改变了,为什么加了个where条件结果就会变呢?
表示想不通啊。。。。。
没办法,只好baidu了,baidu了半天,都没人给个解释啊。。。。。
后来同事说,换个写法试试,于是改了另一种写法,如下:
select * from (select t.*, rownum rn from (select t_e_id, t_e_name, t_e_tel, t_e_areacode from (select t.eid t_e_id, t.ename t_e_name, t.etel t_e_tel, t.areaid t_e_areacode, t.biz_delete_time, decode(areaid, '0730', '0', '1') orderseq from vr_enterprise t where t.eid not in (select eid from t_biz_erelation)) order by orderseq, biz_delete_time nulls last) t) m where m.rn >= 1 and m.rn < 25
这个方法果然凑效,凑效是凑效,关键是为什么前面那种方法不行呢?抓问题要刨根问底。
看来baidu不行,得换google搜索了。找呀找找呀找,在oracle的ask tom上,tom详细的介绍了rownum的用法,在这里http://www.oracle.com/technetwork/issue-archive/2006/06-sep/o56asktom-086197.html
One important thing about using this pagination query is that the ORDER BY statement should order by something unique. If what you are ordering by is not unique, you should add something to the end of the ORDER BY to make it so.
看到这样一句话,原来用order by的时候还需要增加order by的唯一性,不然rownum就会有问题。
相关推荐
oracle 分页查询 使用ROWNUM技巧及陷阱
对于 Oracle 的 rownum 问题,很多资料都说不支持>,>=,=,between...and,只能用以上符号(<、、!=),并非说用>,>=,=,between..and 时会提示SQL语法错误,而是经常是查不出一条记录来,还会出现似乎是莫名其妙的结果来...
oracle rownum和distinct
ORACLE 中ROWNUM用法总结 整理版本,详细明了
内涵oracle rownum的详细讲解。
Oracle中rownum的使用
在MySQL中,我们通常都使用limit来完成数据集获取的分页操作,而在Oracle数据库中,并没有类似limit一样的方便方法来实现分页,因此我们通常都是直接在SQL语句中完成分页,这里就需要借助于rownum伪列或row_number()...
写好oracle的连接字符串和查询语句,调用程序中的方法可以很方便的实现分页功能。该方法中,将参数连接字符串,查询的sql语句,指定每页显示多少行,调用成功后,会返回页数,行数,还有查询的结果数据集。 使用...
Oracle学习笔记(rownum和rowid),有具体的代码案例讲解rownum和rowid
关于oracle的rownum关于oracle的rownum关于oracle的rownum关于oracle的rownum关于oracle的rownum
注意:对 rownum(伪列) 只能使用 < 或 <=, 而用 =, >, >= 都将不能返回任何数据 例:查询工资前20名的员工姓名,工资 ,工资由高到低 Select rownum,first_name,salary from (Select first_name, salary ...
使用数据库时有时我们会需要分页查询,但是在oracle中使用查询条件时又不可以使用大于号>。
select * from (select a.*,rownum rn from (select * from tablename) a where rownum) where rn>2
rownum是Oracle数据库中的一个特有关键字,返回的是一个数字代表记录的行号。这篇文章主要介绍了oracle 使用rownum的三种分页方式,需要的朋友可以参考下
oracle rownum
oracle rownum 使用小技巧 里面包含分页
分页查询格式: SELECT * FROM ( SELECT A.*, ROWNUM RN FROM (SELECT * FROM TABLE_NAME) A WHERE ROWNUM ) WHERE RN >= 21 其中最内层的查询SELECT * FROM TABLE_NAME表示不进行翻页的原始查询语句。ROWNUM ...
Oracle Rownum的使用与JSP分页显示的实现.pdf
举例介绍在oracle中rownum和rowId的不同以及使用方法