赞助商

MOST RECENT NEWS - WE MOMENTARILY RENEW

13
在Oracle中,要按特定条件查询前N条记录,用个rownum就搞定了。 select * from emp where rownum <= 5
那我要取N记录之后的呢?是不是rownum用">" 即 select * from emp where rownum > 5 就搞定了呢??
答案是否定的!
先看看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.


就想java的类rowset一样,使用过后,查询出的行已经被丢弃,所以,这样不能持久的使用。所以,只有在rownum=1时才有用。  

对于rownum来说它是oracle系统顺序分配为从查询返回的行的编号,返回的第一行分配的是1,第二行是2,依此类推,这个伪字段可以用于限制查询返回的总行数,而且rownum不能以任何表的名称作为前缀。

设定一个表company,有13行记录,要查3到10行,怎么查呢?都知道在mysql里面有limit,很好用,在oracle时就要使用子查询了。

如:
SELECT * FROM (SELECT ROWNUM NO FROM COMPANY) WHERE NO BETWEEN 3 AND 10

这样就ok了,如使用
SELECT ROWNUM FROM COMPANY WHERE ROWNUM   BETWEEN 3 AND 10

这样就是不对的。很小的问题,大家记住。:)

作者:博爱老头@博爱老头的草屋
地址:http://www.icnote.com/How-to-user-Oracle-Rownum/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!

0 条评论 to “oracle rownum 使用注意事项”

发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称  [注册]
密码  [游客无需密码]
网址
电邮
               

 

©2007-2012 IcNote.COM Theme by Youth work room