sql (oracle) 选择前 10 条记录,然后选择下 10 条,依此类推
我想我可能只是缺少一些明显的语法,但是选择前 10 条记录、然后是下 10 条记录的 sql (oracle) 是什么?
我尝试使用 rownum 但似乎可以得到 rownum > X 和 rownum < Y 去工作。
LLCF
I figure I might just be missing some obvious syntax but what is the sql (oracle) to select the first 10 records, then the next 10, and so on?
I tried using rownum but can seem to get rownum > X and rownum < Y to work.
llcf
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只有一种相当复杂的方法来做到这一点,这对 Oracle 来说是一个真正的痛苦。他们应该只实现一个 LIMIT/OFFSET 子句...
在 where 子句选择行之后,rownum 被分配,因此 rownum 必须始终以 1 开头。
where rownum > ; x
将始终评估为 false。另外,rownum 在排序完成之前被分配,因此 rownum 的顺序不会与您的 order by 所说的顺序相同。
您可以使用子选择来解决这两个问题:
如果您不需要排序(但仅此而已),您可以简化为
There is only a rather convoluted way to do this, which is a real pain with Oracle. They should just implement a LIMIT/OFFSET clause...
The rownum gets assigned after the row has been selected by the where clause, so that a rownum must always start with 1.
where rownum > x
will always evaluate to false.Also, rownum gets assigned before sorting is done, so the rownum will not be in the same order as your order by says.
You can get around both problems with a subselect:
If you do not need to sort (but only then), you can simplify to
您可以使用 ROW_NUMBER() 函数...例如
You could use ROW_NUMBER() function... for example
您还可以选择所有行,并且一次仅获取 10 行。当然,只有当您可以将光标保持在提取之间时,这才有效。
You could also select all rows, and only fetch 10 at a time. This works only if you can keep the cursor between the fetches, of course.