关于“rownum”的澄清
我有一个表 Table1
Name Date
A 01-jun-2010
B 03-dec-2010
C 12-may-2010
当我使用以下查询查询该表时,
select * from table1 where rownum=1
我得到的输出为
Name Date
A 01-jun-2010
但以同样的方式,当我使用以下查询时,我没有得到任何输出。
select * from table1 where rownum=2
select * from table1 where rownum=3
有人请指导我为什么它会这样工作,以及如何使用 rownum。
I have a table Table1
Name Date
A 01-jun-2010
B 03-dec-2010
C 12-may-2010
When i query this table with the following query
select * from table1 where rownum=1
i got output as
Name Date
A 01-jun-2010
But in the same way when i use the following queries i do not get any output.
select * from table1 where rownum=2
select * from table1 where rownum=3
Someone please give me guidance why it works like that, and how to use the rownum.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
汤姆对于许多 Oracle 相关问题都有答案
简而言之,rownum 在应用 where 子句之后和应用 order by 子句之前可用。
在 RowNum=2 的情况下,where 子句中的谓词永远不会计算为 true,因为 RowNum 从 1 开始,并且只有在可以找到与谓词匹配的记录时才会增加。
Tom has an answer for many Oracle related questions
In short, rownum is available after the where clause has been applied and before the order by clause is applied.
In the case of RowNum=2, the predicate in the where clause will never evaluate to true as RowNum starts at 1 and only increases if records matching the predicate can be found.
添加 rownums 是从数据库获取结果集后最后完成的事情之一。这意味着第一行将始终具有 rownum 1。当您想要限制结果集时(例如进行分页时),最好使用 rownum。
有关更多信息,请参阅:http://www.orafaq.com/wiki/ROWNUM
Adding rownums is one of the last things done after the result set has been fetched from the database. This means that the first row will always have rownum 1. Rownum is better used when you want to limit the result set, for instance when doing paging.
See this for more: http://www.orafaq.com/wiki/ROWNUM
(无论如何都不是 Oracle 专家)
据我了解,rownum 对结果集中的行进行编号。
那么,在您的示例中:
结果集中将有多少行?因此,将为这样的行分配多少 rownum?现在您能明白为什么实际上没有返回结果了吗?
一般来说,您应该避免依赖 rownum 或任何暗示结果顺序的功能。尝试考虑使用整组结果。
话虽这么说,我相信以下方法会起作用:
因为在这种情况下,您要对子查询中的行进行编号。
(Not an Oracle expert by any means)
From what I understand, rownum numbers the rows in a result set.
So, in your example:
How many rows are there going to be in the result set? Therefore, what rownum would be assigned to such a row? Can you see now why no result is actually returned?
In general, you should avoid relying on rownum, or any features that imply an order to results. Try to think about working with the entire set of results.
With that being said, I believe the following would work:
Because in that case, you're numbering the rows within the subquery.