[Database Learning] Paging Query

Table of contents


illustrate

Paging query is to allow us to accurately query a certain part of the data. It can be understood in the same way as what we do on the next page of the web page. How many pieces of data are queried on one page, and which row of data starts to be displayed, are all paging. Query can be achieved.

grammar

1. mysql syntax

Explanation: limit is the pagination syntax in mysql, but it also limits the number of query results
Note: the number after limit must be an integer!

- specify the initial position

select 字段名 from 数据表名  limit 初始位置,记录条数

The initial position starts from 0

- Do not specify initial position

limit 记录数

"Number of records" indicates the number of records displayed. If the value of "Number of Records" is less than the total number of query results, the specified number of records will be displayed starting from the first record. If the value of "Number of Records" is greater than the total number of query results, all the records that are queried will be displayed directly.

- Use with offset

limit 记录数 offset 初始位置

2. SQL server syntax

The original author has the code to check the performance, so if you are interested, you can go to the link at the end of the article.

-- 设置执行时间开始,用来查看性能的
set statistics time on ;

- triple loop

1. First fetch the first 20 pages, then reverse order, and fetch the first 10 records in reverse order. In this way, you can get the data required for paging, but the order is reversed. You can then return it in reverse order, or you can stop sorting and hand it over directly. Front-end sorting.

-- 分页查询(top)
select * 
from (select top pageSize * 
from (select top (pageIndex*pageSize) * 
from 数据表 
order by 字段id asc ) -- 其中里面这层,必须指定按照升序排序,省略的话,查询出的结果是错误的。
as temp_sum_student -- 另命名
order by 字段id desc ) temp_order
order by 字段id asc

-- 分页查询第2页,每页有10条记录
select * 
from (select top 10 * 
from (select top 20 * 
from 数据表 
order by sNo asc ) -- 其中里面这层,必须指定按照升序排序,省略的话,查询出的结果是错误的。
as temp_sum_student 
order by 字段id desc ) temp_order
order by 字段id asc
;

2. First query the first 10 records, then use not in to exclude these 10 records, and then query. No code.

- use max (primary key)

First, top the first 11 row records, then use max(id) to get the largest id, and then query the top 10 records again in this table, but add the condition, where id>max(id).

-- 分页查询(max)
select top pageSize * 
from 数据表 
where 字段id>=
(select max(字段id) 
from (select top ((pageIndex-1)*pageSize+1) 字段id
from 数据表 
order by  字段id asc) temp_max_ids) 
order by 字段id;
-- 分页查询第2页,每页有10条记录
select top 10 * 
from 数据表 
where 字段id>=
(select max(字段id) 
from (select top 11 字段id
from 数据表 
order by 字段id asc) temp_max_ids) 
order by 字段id;

- Use the row_number keyword (only available in version 2005 and above)

Directly use the row_number() over(order by id) function to calculate the number of rows, select the corresponding row number and return it

-- 分页查询(row_number)
select top pageSize * 
from (select row_number() 
over(order by 字段id asc) as rownumber,* 
from 数据表 ) temp_row
where rownumber>((pageIndex-1)*pageSize);

-- 分页查询第2页,每页有10条记录
select top 10 * 
from (select row_number() 
over(order by 字段id asc) as rownumber,* 
from 数据表 ) temp_row
where rownumber>10;

- offset /fetch next (only available in 2012 and above)

offset A rows, discard the previous A records, fetch next B rows only, and read B records backward.

-- 分页查询(offset /fetch next)
select * from 数据表
order by 字段id 
offset ((@pageIndex-1)*@pageSize) rows
fetch next @pageSize rows only;

-- 分页查询第2页,每页有10条记录
select * from 数据表
order by 字段id  
offset 10 rows
fetch next 10 rows only ;

Explanation: The original author recommends the last one. The performance of the first one is very poor. I have no practical experience, so I can’t judge it, so I copy it.

3. Orcale syntax

- using the pseudo-column rownum

1. Query the first 10 records

select * from 数据表 where ROWNUM <10;

2. According to the ID ranking, when grabbing the first three records,
you cannot use > (a value greater than 1), >= (a value greater than or equal to 1), = (a value greater than or equal to 1) for ROWNUM, otherwise there will be no result, so directly It can only start from 1 and
there is no record for rownum >10, because if the first one is not satisfied and removed, the rownum of the second one will become 1 again, so there will never be a record that satisfies the condition.

select * from 数据表 where rownum>=1;

3. If you want to use rownum not starting from 1, you need to use the following method

select 表别名.* 
from (select 数据表.*,rownum rn from 数据表) 表别名 
where rn >5;

- Pagination query one

select * from (
select 表别名.*,rownum rn from (
select * from 数据表) 表别名
where rownum <=5) 
where rn>=2;

- Pagination query 2

select 表别名.* from (
select 数据表.*,rownum rn 
from 数据表 
where rownum <=5) 表别名 
where rn >=3;

- Pagination query three

select 表别名.* from (
select 数据表.*,rownum rn 
from 数据表) 表别名 
where rn between 3 and 5;

Because I haven't studied oracle, it is intercepted from other parts, please refer to other parts.

reference link

1. MySQL LIMIT: Limit the number of query results
2. Database paging query
3. Four methods of SQL server paging (considered very comprehensive)
4. Three methods of Oracle database paging

Guess you like

Origin blog.csdn.net/Daisy74RJ/article/details/124016454