您的位置:首页 > 数据库 > Oracle

oracle 分页 数据重复 数据不正确

2015-04-16 00:00 190 查看
摘要: oracle 分页 中一般使用rownum 分页 如果有排序话 就可能出现问题

oracle 一般分页的写法为

select a.id,a.title,rownum as rn,a.create_time from
(select a.* from temp a order by create_time desc) a  where rownum<=20

注意:我用的是 create_time 来进行排序 create_time 中有相等的
为了对比分页数据的差异我用left join 来看数据是否正常
这个是用的一个分页 我用了关联来对比分20条数据和10的数据之间的差异

select a.title,a.rn,b.title,b.rn from
(select a.id,a.title,rownum as rn,a.create_time from (select a.* from temp a order by create_time desc) a where rownum<=20) a
left join
(select a.id,a.title,rownum as rn,a.create_time from
(select a.* from temp a order by create_time desc) a where rownum<=10) b
on a.id=b.id

执行结果



按道理是右边前面10条会和左边前面10条数据一样 但是运行后是不一样的

如果这样的话 会有在翻页钟可能出现一条记录会在多个页面出现的现象 甚至多个页面数据一样

sql调整后 在嵌套一层

select a.* from
(select a.id,a.title,rownum as rn,a.create_time from
(select a.* from temp a order by create_time desc) a) a  where a.rn<=20

select a.title,a.rn,b.title,b.rn from
(select a.* from (select a.id,a.title,rownum as rn,a.create_time from (select a.* from temp a order by create_time desc) a) a where a.rn<=20) a
left join
(select a.* from
(select a.id,a.title,rownum as rn,a.create_time from
(select a.* from temp a order by create_time desc) a) a where a.rn<=10) b
on a.id=b.id

执行结果对比



这样结果 就对的上了

有没有发现虽然是相同的sq 套了一层select后结果集都变了 前后两张图片的 左列结果都不是一样的

总结 我这里用的时间排序 其中时间相等的数据很多 如果排序字段唯一 应该不会有这种问题出现
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息