Oracle paging sorting

Source: Internet
Author: User

OracleFour Common query statements for paging and sorting

IT maopu technology Source: itmop.com Author: Edit finishing CLICK: 318 times

In oracle, how does one implement the paging and sorting functions of query statements? The implementation method may be different from that of ms SQL Server. This is mainly because of the similarities and differences in the processing of result row numbers in the two databases. In ms SQL Server, you can use the combination of the top and ROW_NUMBER () methods to achieve this. Generally, the writing method is relatively simple, while oracle is very different. It can only be implemented through ROWNUM. Here we will mainly describe it here, how to Use ROWNUM to implement some common paging and sorting queries? For the concept of ROWNUM, you can search for it at will. There are a lot of detailed explanations, so I will not go into details.

1. query the first 10 records

SELECT * FROM TestTable where rownum <= 10

2. query 11th to 20th records

SELECT * FROM (SELECT TestTable. *, ROWNUM ro FROM TestTable where rownum <= 20) WHERE ro> 10

3. The first 10 records sorted by name field in ascending order

SELECT * FROM (SELECT * FROM TestTable ordery by name ASC) where rownum <= 10

4. 11th to 20th records sorted by name field in ascending order

SELECT * FROM (SELECT tt. *, ROWNUM ro FROM (SELECT * FROM TestTable order by name ASC) tt where rownum <= 20) WHERE ro> 10

The preceding statement may be the most commonly used statement in query sorting and paging. No conditions are added because the statement structure is not affected. Although the performance may be affected, it may only be possible to query more data in the innermost layer of the query, but it should have little impact on the re-sorting and re-selection of the outer layer.

 

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.