Three solutions for SQL paging statements

Source: Internet
Author: User

Method 1:

Copy codeThe Code is as follows:
Select top page size *
FROM table1
WHERE id NOT IN
(
Select top page size * (page-1) id FROM table1 order by id
)
Order by id

Method 2:

Copy codeThe Code is as follows:
Select top page size *
FROM table1
WHERE id>
(
Select isnull (MAX (id), 0)
FROM
(
Select top page size * (page-1) id FROM table1 order by id
)
)
Order by id

METHOD Two reverse order:

Copy codeThe Code is as follows:
Select top page size *
FROM table1
Where id <=
(
Select isnull (MIN (ID), (select max (ID) FROM table1 ))
FROM
(
Select top (page size * (page size-1) id from tbl_files ORDER BY ID DESC
)
)
ORDER BY ID DESC

Method 3

Copy codeThe Code is as follows:
Select top page size *
FROM
(
SELECT ROW_NUMBER () OVER (order by id) AS RowNumber, * FROM table1
)
WHERE RowNumber> page size * (page number-1)

Paging solution 2: (using more than ID and select top pages) is the most efficient. You need to splice an SQL statement
Paging solution 1: (using Not In and select top pages) The efficiency is second, and SQL statements need to be spliced.
Paging solution 3: (using SQL cursor Stored Procedure paging) The efficiency is the worst, but the most common

PS: these three solutions are found on the Internet. When paging is performed, solution 2 is selected. It is found that the directly applied statements cannot meet the requirements, because they need to be sorted in reverse order according to the posting order, that is, the latest posts need to be arranged at the beginning, so we changed the order to reverse order based on solution 2, hoping to help you.

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.