Oracle 中分页的 LIMIT 和 OFFSET 的替代方案

发布于 2024-09-03 04:43:17 字数 574 浏览 8 评论 0原文

我正在开发一个网络应用程序,需要对有序结果进行分页。我通常使用 LIMIT/OFFSET 来达到此目的。

在 Oracle 中对有序结果进行分页的最佳方式是什么?我见过一些使用 rownum 和子查询的示例。是这样吗?您能给我一个将此 SQL 转换为 Oracle 的示例吗:(

SELECT fieldA,fieldB 
FROM table 
ORDER BY fieldA 
OFFSET 5 LIMIT 14

我正在使用 Oracle 10g,因为它的价值)

谢谢!


答案: 使用 karim79 提供的以下链接,此 SQL 将如下所示:

SELECT * FROM (
    SELECT rownum rnum, a.* 
    FROM(
        SELECT fieldA,fieldB 
        FROM table 
        ORDER BY fieldA 
    ) a 
    WHERE rownum <=5+14
)
WHERE rnum >=5

I'm developing a web application and need to page ordered results. I normaly use LIMIT/OFFSET for this purpose.

Which is the best way to page ordered results in Oracle? I've seen some samples using rownum and subqueries. Is that the way? Could you give me a sample for translating this SQL to Oracle:

SELECT fieldA,fieldB 
FROM table 
ORDER BY fieldA 
OFFSET 5 LIMIT 14

(I'm using Oracle 10g, for what it's worth)

Thanks!


Answer:
Using the link provided below by karim79, this SQL would look like:

SELECT * FROM (
    SELECT rownum rnum, a.* 
    FROM(
        SELECT fieldA,fieldB 
        FROM table 
        ORDER BY fieldA 
    ) a 
    WHERE rownum <=5+14
)
WHERE rnum >=5

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

或十年 2024-09-10 04:43:17

从 oracle 12c 开始,您可以使用前 N 个查询。

SELECT fieldA,fieldB 
FROM table 
ORDER BY fieldA 
OFFSET 5 ROWS FETCH NEXT 14 ROWS ONLY;

http://www .oracle-base.com/articles/12c/row-limiting-clause-for-top-n-queries-12cr1.php

As of oracle 12c, you could use the top N queries.

SELECT fieldA,fieldB 
FROM table 
ORDER BY fieldA 
OFFSET 5 ROWS FETCH NEXT 14 ROWS ONLY;

http://www.oracle-base.com/articles/12c/row-limiting-clause-for-top-n-queries-12cr1.php

我不会写诗 2024-09-10 04:43:17

由于您使用的是 10g,因此您应该能够使用分析函数来简化 ROWNUM 方法

SELECT fieldA, 
       fieldB
  FROM (SELECT fieldA,
               fieldB,
               row_number() over (order by fieldA) rnk
          FROM table_name)
 WHERE rnk BETWEEN 5 AND 14;

Since you're on 10g, you should be able to simplify the ROWNUM approach using analytic functions

SELECT fieldA, 
       fieldB
  FROM (SELECT fieldA,
               fieldB,
               row_number() over (order by fieldA) rnk
          FROM table_name)
 WHERE rnk BETWEEN 5 AND 14;
我要还你自由 2024-09-10 04:43:17

方法 1:对于数据库版本 Oracle12c 或更高版本

SELECT fieldA, fieldB 
FROM table 
ORDER BY fieldA 
OFFSET 5 ROWS FETCH NEXT 14 ROWS ONLY

方法 2:对于数据库版本 Oracle11g 或更低版本,使用分析函数 RowNumber()

SELECT fieldA, fieldB
FROM (
    SELECT fieldA, fieldB,
        row_number() over (order by fieldA) rowRank
    FROM table_name
)
WHERE rowRank BETWEEN 5 AND 14;

方法 3:对于数据库版本 Oracle11g 或更低版本,使用 RowNum

SELECT T.* FROM ( 
SELECT T.*, rowNum as rowIndex
FROM (
    SELECT fieldA, fieldB,
    FROM table_name
)T)T
WHERE rowIndex > 0 AND rowIndex <= 20;

在某些情况下,我发现方法 3 更快优于方法 2,因为 order by 子句在方法 2 中是强制性的。但是,如果您的数据库版本是 12c 或更高版本,则必须使用方法 1。

Method-1: For database version Oracle12c or higher

SELECT fieldA, fieldB 
FROM table 
ORDER BY fieldA 
OFFSET 5 ROWS FETCH NEXT 14 ROWS ONLY

Method-2: For database version Oracle11g or lower using analytical function RowNumber()

SELECT fieldA, fieldB
FROM (
    SELECT fieldA, fieldB,
        row_number() over (order by fieldA) rowRank
    FROM table_name
)
WHERE rowRank BETWEEN 5 AND 14;

Method-3: For database version Oracle11g or lower using RowNum

SELECT T.* FROM ( 
SELECT T.*, rowNum as rowIndex
FROM (
    SELECT fieldA, fieldB,
    FROM table_name
)T)T
WHERE rowIndex > 0 AND rowIndex <= 20;

In some cases, I have found method-3 is faster than method-2 since order by clause is mandatory in method 2. However, if your database version is 12c or higher you must go for method-1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文