Home Database Mysql Tutorial Oracle中的rownum不能使用大于的问题

Oracle中的rownum不能使用大于的问题

Jun 07, 2016 pm 05:08 PM

关于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 of this Website
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

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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1671
14
PHP Tutorial
1276
29
C# Tutorial
1256
24
MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Explain the role of InnoDB redo logs and undo logs. Explain the role of InnoDB redo logs and undo logs. Apr 15, 2025 am 12:16 AM

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 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.

How does MySQL index cardinality affect query performance? How does MySQL index cardinality affect query performance? Apr 14, 2025 am 12:18 AM

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

MySQL for Beginners: Getting Started with Database Management MySQL for Beginners: Getting Started with Database Management Apr 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

MySQL vs. Other Databases: Comparing the Options MySQL vs. Other Databases: Comparing the Options Apr 15, 2025 am 12:08 AM

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

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: Structured Data and Relational Databases MySQL: Structured Data and Relational Databases Apr 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.

See all articles