Access database query pagination method

For the situation where there are many records in the database, paging is required when querying. The following describes the two paging methods of the Access database. Other databases seem to have better methods. Here we only discuss the Access database.

1 Access query result pagination method 1

1.1 Method

Let the number of records per page be CntPerPage. To obtain the query results of page 1, use the following SQL statement.

SELECT * FROM (
SELECT TOP CntPerPage * FROM(
SELECT TOP Page*CntPerPage * FROM TableName WHERE Query_Conditionm ORDER BY OrderField
) ORDER BY OrderField DESC
) ORDER BY OrderField

Among them, TableName is the table name, Query_Condition is the query condition, and OrderField is the field name used for sorting

1.2 Principle

Nested use of three-step query:
the first step of query: query the records of the current page and all previous pages, and sort them in descending order according to the sort field

SELECT TOP Page*CntPerPage * FROM TableName WHERE Query_Conditionm ORDER BY OrderField

The second step of query: Query the first CntPerPage records of the records just queried (sorted in descending order) (keep descending order), these are all the records of Page

SELECT TOP CntPerPage * FROM( 第一步查询结果 ) ORDER BY OrderField DESC

The third step of query: the previous step of the query has actually queried all the records of the Page page, but they are arranged in descending order. The third step is just to change the data just obtained into a positive order. If the order is not required, or there are other data sorting processes in the program, then this step can be omitted.

SELECT * FROM ( 第二步查询结果 ) ORDER BY OrderField

1.3 Examples

A Student table, to query all records whose age is less than 20 years old, sort by name, the number of records per page is 100, to obtain the query results on page 5, use the following SQL statement.

SELECT * FROM (SELECT TOP 100 * FROM( SELECT TOP 500 * FROM Student  WHERE age<20 ORDER BY name ) ORDER BY name DESC) ORDER BY name

2 Commonly used NOT IN methods on the Internet

This is the most common method that comes up when searching for pagination methods on the web.
method:

SELECT TOP CntPerPage * FROM TableName WHERE Query_Conditionm AND id NOT IN (
SELECT TOP (Page-1)*CntPerPage id FROM TableName WHERE Query_Conditionm ORDER BY id
) ORDER BY id

Principle:
Two-step query:
Step 1: Select all eligible record ids before this page

SELECT TOP (Page-1)*CntPerPage id FROM TableName WHERE Query_Conditionm ORDER BY id

Step 2: Add the query condition "id is not in the id query results selected just now", and take the first CntPerPage records, so that the query results just filter all the data of the previous Page-1 page, and then select the first Page All records on the page.

SELECT TOP CntPerPage * FROM TableName WHERE Query_Conditionm AND id NOT IN (第一步查询结果) ORDER BY id

3 Paging query efficiency

The actual test efficiency of the two methods is similar, but I personally think that the efficiency of the NOT IN judgment in the second method should not be too high, so I think the first method should be more efficient if the amount of records is relatively large.

Guess you like

Origin blog.csdn.net/hangl_ciom/article/details/98473409