Example of Oracle three-layer nested paging query and rownum principle, oraclerownum

Source: Internet
Author: User

Example of Oracle three-layer nested paging query and rownum principle, oraclerownum

For example, the data in the COMPONENT table is as follows:

1. Execute select * from (select com. *, rownum r_num from (select * from COMPONENT) com where rownum <8) where r_num> 2

2. Execute select * from (select com. *, rownum r_num from (select * from COMPONENT) com where rownum = 1) where r_num <5

3. select * from (select com. *, rownum r_num from (select * from COMPONENT) com where rownum = 2) where r_num <5

 

Cause:

Rownum is the amount dynamically generated by oracle Based on the query. It does not look at the where Condition Clause before generating rownum. Therefore, when querying a result, rownum is initialized to 1 by oracle, and then the where clause is executed,

If it is true, it will continue. Otherwise, it will be discarded, which is the reason why rownum = 1 rownum <n is good.

Rownum = 2 is not good because:

1. Perform the query operation

2. Set row num of the first row to 1.

3. Compare the row num of the row with the condition. If the row num does not match, the row is discarded. If the row num matches, the row is returned.

4. oracle obtains the next row and adds rownum to 1.

5. Return to Step 1

Similarly, select rownum, id from COMPONENT where rownum> 2 does not return rows, because the select id from COMPONENT is queried first, and the rownum of row 1st is marked as 1, and then the where condition is viewed, if the value of 1 <2 is false, the row is discarded. If the value of 2nd rows is executed, or if the value of rownum is 1, the where condition or 1 <2 is false. Therefore, it is always false, and rownum does not change, all rows are discarded, so there is no result.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.