Oracle中的伪列

分页查询中,需要用到伪列rownum,代码如下:

 select * from (select rownum rn, name from cost where rownum <= 6) where rn >3;

可是第一次用rownum,第二次用rn,位置不能变,否则出错,第一次的rownum是oracle中的一个虚拟列,
rn是给这个rownum起的别名,也就是在子查询中的别名要在外查询中调用。需要注意一下,就是rownum是取出一个再编号,

所以在里面我们可以写<或者<=,而不能写成>或者>=,因为取出一个才编号,要想取出大于3的,就必须先取出1,2,3的,因为按顺序编号。

如果先按工资排序,再取出6到10号,可以写成:

select * from (select rownum rn, t.* from (select * from cost order by sal desc) t where rownum < 11) where rn >= 6;

同样注意什么时候用rownum,什么时候用rn.

这样写是不被允许的:

select rownum rn from cost where rn = 1;

要写成:

select rownum rn from cost where rownum = 1;

原文地址:https://www.cnblogs.com/DarrenChan/p/5562830.html