SQL ROWNUM 如何返回特定范围之间的行
如何返回特定范围的 ROWNUM
值?
我正在尝试以下操作:
select * from maps006 where rownum >49 and rownum <101
这仅返回与 <
运算符匹配的行。
How can I return a specific range of ROWNUM
values?
I'm trying the following:
select * from maps006 where rownum >49 and rownum <101
This returns only rows matching the <
operator.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
请注意双重嵌套视图。
ROWNUM
在ORDER BY
之前计算,因此它是正确编号所必需的。如果省略
ORDER BY
子句,您将无法获得一致的顺序。Note the double nested view.
ROWNUM
is evaluated beforeORDER BY
, so it is required for correct numbering.If you omit
ORDER BY
clause, you won't get consistent order.我知道这是一个老问题,但是,提及最新版本中的新功能很有用。
从Oracle 12c开始,您可以使用新的Top-n Row限制功能。无需编写子查询,不依赖 ROWNUM。
例如,以下查询将按升序返回薪水第四高到第七高之间的员工:
I know this is an old question, however, it is useful to mention the new features in the latest version.
From Oracle 12c onwards, you could use the new Top-n Row limiting feature. No need to write a subquery, no dependency on ROWNUM.
For example, the below query would return the employees between 4th highest till 7th highest salaries in ascending order:
我一直在寻找解决方案,发现这很棒
解释解决方案的文章
相关摘录
现在有一个真实的例子(获取第 148、149 和 150 行):
I was looking for a solution for this and found this great
article explaining the solution
Relevant excerpt
Now with a real example (gets rows 148, 149 and 150):
您还可以使用 CTE with 子句。
You can also do using CTE with clause.