search
HomeDatabaseMysql TutorialOracle中的rownum不能使用大于的问题

关于Oracle 的 rownum 问题,很多资料都说不支持SQL语句中的ldquo;gt;、gt;=、=、between...andrdquo;运算符,只能用如下运算

一、对rownum的说明

   关于Oracle 的 rownum 问题,很多资料都说不支持SQL语句中的“>、>=、=、between...and”运算符,只能用如下运算符号“

   并非说用“>、>=、=、between..and”时会提示SQL语法错误,而是经常是查不出一条记录来,还会出现似乎是莫名其妙的结果来。

   其实,只要理解好了这个 rownum 伪列的意义就不应该感到惊奇。

   rowid 与 rownum 虽都被称为伪列,但它们的存在方式是不一样的:

   rowid 是物理存在的,表示记录在表空间中的唯一位置ID,在DB中是唯一的。只要记录没被搬动过,rowid是不变的。

   rowid 相对于表来说又像表中的一般列,所以,以 rowid 为条件就不会有rownum那些莫名其妙的结果出现。

   另外还要注意:rownum不能以任何基表的名称作为前缀。

   对于下面的SQL语句

   SQL>select rownum,id,age,name from loaddata where rownum > 2;

    ROWNUM ID     AGE NAME
    ------- ------ --- ------

    rownum>2,没有查询到任何记录。

    因为rownum总是从1开始的,第一条不满足去掉的话,,第二条的rownum 又成了1。依此类推,所以永远没有满足条件的记录。

    可以这样理解:rownum是一个序列,是Oracle数据库从数据文件或缓冲区中读取数据的顺序。

    它取得第一条记录则rownum值为1,第二条为2。依次类推。

    当使用“>、>=、=、between...and”这些条件时,从缓冲区或数据文件中得到的第一条记录的rownum为1,不符合sql语句的条件,会被删除,接着取下条。

    下条的rownum还会是1,又被删除,依次类推,便没有了数据。


二、对rownum使用中几种现象的分析说明
 
    有了以上从不同方面建立起来的对rownum的概念,下面认识使用rownum的几种现象:

    (1) select rownum,id,age,name from loaddata where rownum != 10 为何是返回前9条数据呢?
     为什么它与 select rownum,id,age,name from loaddata where rownum

     因为是在查询到结果集后,显示完第9条记录后,之后的记录都是 != 10或者 >=10,所以只显示前面9条记录。

     也可以这样理解,rownum为9后,取的记录的rownum为10,因条件为 !=10,所以删掉。然后取下一条,其rownum又是10,也删掉。以此类推。

     所以只会显示前面9条记录。


    (2)什么rownum >1时查不到一条记录,而 rownum >0或rownum >=1 却总显示所有记录。

     这是因为rownum是在查询到的结果集后,再加上去的,它总是从1开始的。


    (3)为什么between 1 and 10 或者 between 0 and 10 能查到结果,而用 between 2 and 10 却得不到结果。

     原因同上:因为 rownum总是从1开始。


     从上可得,任何时候想把rownum = 1这条记录抛弃是不对的。它在结果集中是不可或缺的。

     少了rownum=1就像空中楼阁一般不能存在。所以,rownum条件要包含到1。


三、一些rownum实际运用的例子:

     -----------
     --sql建表脚本

     create table LOADDATA
     (
         ID   VARCHAR2(50),
         AGE  VARCHAR2(50),
         NAME VARCHAR2(50)
     );
     -----------

    (1) rownum 对于“等于某值”的查询条件

     如果希望找到loaddata表中第一条记录的信息,可以使用rownum=1作为条件。

     但是想找到loaddata表中第二条记录的信息,使用rownum=2,是查不到数据的。

     因为rownum都是从“1”开始。

     “1”以上的自然数,在rownum做等于判断是时认为都是false条件,所以无法查到rownum = n(n>1的自然数)。


      select rownum,id,age,name
      from loaddata
      where rownum = 1;   --可以用在限制返回记录条数的地方,保证不出错,如:隐式游标。


    SQL>select rownum,id,age,name from loaddata where rownum = 1;

    ROWNUM ID     AGE NAME
    ------- ------ --- ------
         1 200001 22   AAA


    SQL>select rownum,id,age,name from loaddata where rownum = 2;

    ROWNUM ID     AGE NAME
    ------- ------ --- ------


   注:SQL>select rownum,id,age,name from loaddata where rownum != 3; --返回的是前2条记录。

     ROWNUM ID     AGE NAME
    ------- ------ --- ------
         1 200001 22   AAA
         2 200002 22   BBB


  (2)rownum对于大于某值的查询条件

    如果想找到从第二行记录以后的记录,当使用rownum>2是查不出记录的。

    原因是由于rownum是一个总是从1开始的伪列,Oracle 认为rownum> n(n>1的自然数)这种条件依旧不成立,所以查不到记录。

    SQL>select rownum,id,age,name from loaddata where rownum > 2;

    ROWNUM ID     AGE NAME
    ------- ------ --- ------


    那如何才能找到第二行以后的记录?

    可以使用下面的子查询方法来解决。

    注意子查询中的rownum必须要有别名,否则仍然会查不到记录,这是因为rownum不是某个表的列。

    如果不起别名的话,无法知道rownum是子查询的列,还是主查询的列。


     SQL>select rownum,id,age,name from(select rownum no ,id,age,name from loaddata) where no > 2;

     ROWNUM ID     AGE NAME
     ------- ------ --- ------
         3 200003 22   CCC
         4 200004 22   DDD
         5 200005 22   EEE
         6 200006 22   AAA


     SQL>select * from(select rownum,id,age,name from loaddata) where rownum > 2;

     ROWNUM ID     AGE NAME
     ------- ------ --- ------

 

    (3)rownum对于小于某值的查询条件

     如果想找到第三条记录以前的记录,当使用rownum

     显然rownum对于rownum1的自然数)的条件认为是成立的,所以可以找到记录。

     SQL> select rownum,id,age,name from loaddata where rownum

     ROWNUM ID     AGE NAME
     ------- ------ --- ------
         1 200001 22   AAA
         2 200002 22 BBB


     综上几种情况:

     可能有时候需要查询rownum在某区间的数据,从上可以看出rownum对小于某值的查询条件是认为true的。

     rownum对于大于某值的查询条件直接认为是false的,但是可以间接的让它转为认为是true的,那就必须使用子查询。

     例如要查询rownum在第二行到第三行之间的数据,包括第二行和第三行数据,那么只能写以下语句,先让它返回小于等于三的记录行,

     然后在主查询中判断新的rownum的“别名列”大于等于二的记录行。但是这样的操作会在大数据集中影响到检索速度。


     SQL>select * from (select rownum no,id,age,name from loaddata where rownum = 2; --必须是里小外大

     ROWNUM ID     AGE NAME
     ------- ------ --- ------
         2 200002 22 BBB
         3 200003 22   CCC


     也可以用这种方法实现:

     SQL>select rownum,id,age,name from loaddata where rownum          minus
         select rownum,id,age,name from loaddata where rownum

     ROWNUM ID     AGE NAME
     ------- ------ --- ------
         2 200002 22 BBB
         3 200003 22   CCC

linux

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools