Rownum伪例使用的限制:
SQL>; select rownum,month,sell from sale where rownum=1;(可以用在限制返回记录条数的地方,保证不出错,如:隐式游标)
SQL>; select rownum,month,sell from sale where rownum=2;(1以上都查不到记录)
SQL>; select rownum,month,sell from sale where rownum>;5;
(由于rownum是一个总是从1开始的伪列,Oracle 认为这种条件不成立,查不到记录)
所以,使用Rownum限制只能rownum<n的方式
那么如何获得比如5到10行的结果呢?有两种办法:
1、用minus
select rownum,month,sell from sale where rownum<10
minus
select rownum,month,sell from sale where rownum<5;
2、利用子查询,在子查询中通过rownum新创造一个列,然后根据这个列进行限制
Select * From
(Select Rownum row_id,a.*
From boss_user a
Where Rownum<20)
Where Row_id Between 5 And 10;
其中第二行a.*是必须的,不能直接用*来查询
本文介绍了 Oracle 数据库中 ROWNUM 的使用限制及其解决方法,包括如何获取特定范围内的记录,提供了两种实用的方法:使用 minus 和子查询。
512

被折叠的 条评论
为什么被折叠?



