SQL(ORACLE):ORDER BY和LIMIT

时间:2022-12-07 22:48:13

I want do sorting by property ALL data in my db and ONLY AFTER that use LIMIT and OFFSET.

我想要在我的数据库中按属性排序所有数据,并且仅在使用LIMIT和OFFSET之后。

Query like this:

查询如下:

SELECT select_list
    FROM table_expression
    [ ORDER BY ... ]
    [ LIMIT { number | ALL } ] [ OFFSET number ] 

I know the sorting ends as soon as it has found the first row_count rows of the sorted result. Can I do sorting all data before calling LIMIT and OFFSET?

我知道排序一旦找到排序结果的第一个row_count行就结束了。在调用LIMIT和OFFSET之前,我可以对所有数据进行排序吗?

1 个解决方案

#1


54  

Prior to 12.1, Oracle does not support the LIMIT or OFFSET keywords. If you want to retrieve rows N through M of a result set, you'd need something like:

在12.1之前,Oracle不支持LIMIT或OFFSET关键字。如果要检索结果集的行N到M,则需要以下内容:

SELECT a.*
  FROM (SELECT b.*,
               rownum b_rownum
          FROM (SELECT c.*
                  FROM some_table c
                 ORDER BY some_column) b
         WHERE rownum <= <<upper limit>>) a
 WHERE b_rownum >= <<lower limit>>

or using analytic functions:

或使用分析函数:

SELECT a.*
  FROM (SELECT b.*,
               rank() over (order by some_column) rnk
          FROM some_table)
 WHERE rnk BETWEEN <<lower limit>> AND <<upper limit>>
 ORDER BY some_column

Either of these approaches will sort give you rows N through M of the sorted result.

这些方法中的任何一种都将排序,为您提供排序结果的行N到M.

In 12.1 and later, you can use the OFFSET and/or FETCH [FIRST | NEXT] operators:

在12.1及更高版本中,您可以使用OFFSET和/或FETCH [FIRST | NEXT]运营商:

SELECT *
  FROM some_table
 ORDER BY some_column
 OFFSET <<lower limit>> ROWS
  FETCH NEXT <<page size>> ROWS ONLY

#1


54  

Prior to 12.1, Oracle does not support the LIMIT or OFFSET keywords. If you want to retrieve rows N through M of a result set, you'd need something like:

在12.1之前,Oracle不支持LIMIT或OFFSET关键字。如果要检索结果集的行N到M,则需要以下内容:

SELECT a.*
  FROM (SELECT b.*,
               rownum b_rownum
          FROM (SELECT c.*
                  FROM some_table c
                 ORDER BY some_column) b
         WHERE rownum <= <<upper limit>>) a
 WHERE b_rownum >= <<lower limit>>

or using analytic functions:

或使用分析函数:

SELECT a.*
  FROM (SELECT b.*,
               rank() over (order by some_column) rnk
          FROM some_table)
 WHERE rnk BETWEEN <<lower limit>> AND <<upper limit>>
 ORDER BY some_column

Either of these approaches will sort give you rows N through M of the sorted result.

这些方法中的任何一种都将排序,为您提供排序结果的行N到M.

In 12.1 and later, you can use the OFFSET and/or FETCH [FIRST | NEXT] operators:

在12.1及更高版本中,您可以使用OFFSET和/或FETCH [FIRST | NEXT]运营商:

SELECT *
  FROM some_table
 ORDER BY some_column
 OFFSET <<lower limit>> ROWS
  FETCH NEXT <<page size>> ROWS ONLY