各位,我在用rownum来做限定条件值是出现这样的问题:
select rownum,ename from emp;       rownum   ename
1 SMITH
2 ALLEN
3 WARD
4 JONES
5 MARTIN
6 BLAKEselect rownum,ename from emp where rownum=1;       rownum   ename
1 SMITH
select rownum,ename from emp where rownum=2;没有数据select rownum,ename from emp where rownum in(1,2);       rownum   ename
1 SMITH
2 ALLEN
select rownum,ename from emp where rownum in(2);
也是没有数据为什么啊 有没有人告诉我原因啊 我到是可以用
  select p.r,p.ename from (select rownum as r,ename  from emp)p where p.r=5;
       rownum   ename
5 MARTIN
找出特定的, 但上面不能出来结果的原因是啥啊? 谢谢各位了

解决方案 »

  1.   

    rownum机制:1 Oracle executes your query.2 Oracle fetches the first row and calls it row number 1.3 Have we gotten past row number meets the criteria? If no, then Oracle discards the row, If yes, then Oracle return the row.4 Oracle fetches the next row and advances the row number (to 2, and then to 3, and then to 4, and so forth).5 Go to step 3.
      

  2.   

    简单地理解:rownum在输出时才产生的,当有了第一条记录才会有第二条,依次类推.
    所以不能使用>,使用=时只有rownum=1有效.
      

  3.   

    rownum是Oracle的伪(隐含之意)列,每条记录生成时均产生一个,因不是通常的字段,使用时受到了许多限制。详细请参考:
    http://hi.baidu.com/caotanainaide/blog/item/5fac152a76ebf09e023bf603.html
      

  4.   

       rownum 是一个非常特殊的列,用来取代 其它sql 中的 top
       他们两个功能也挺相似的 他们只能显示一段数据 而且默认的起点是1.
       例如:
        select top 1 ename from emp;
       select rownum,ename from emp where rownum=1;    select top 5 ename from emp 
       select rownum,ename from emp where rownum<6;    他们都不能从中间截取,呵呵。
      

  5.   

    rownum=2,因为用的等号,
    这样查到的应该只有一条记录,
    而你要第二条当然没有了,这样的语句逻辑本身就有问题