SQL (ORACLE):ORDER BY 和 LIMIT

SQL (ORACLE): ORDER BY and LIMIT(SQL (ORACLE):ORDER BY 和 LIMIT)
本文介绍了SQL (ORACLE):ORDER BY 和 LIMIT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这样的查询:

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

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

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?

推荐答案

在 12.1 之前,Oracle 不支持 LIMITOFFSET 关键字.如果要检索结果集的第 N 到 M 行,则需要类似以下内容:

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:

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>>

或使用解析函数:

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

这两种方法中的任何一种都会为您提供排序结果的第 N 到 M 行.

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

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

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

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

这篇关于SQL (ORACLE):ORDER BY 和 LIMIT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)