SQL preparation
create table tb_students (
id int auto_increment primary key comment '主键ID',
studentid char(9) unique not null comment '学生学号',
name varchar(10) not null comment '学生姓名',
gender char(1) not null comment '学生性别'
) comment '学生表';
insert into tb_students (studentid , name , gender)
values ('202302301' , '海绵宝宝' , '男'),
('202302302' , '胡图图' , '男'),
('202302303' , '小福贵' , '男'),
('202302304' , '大头儿子' , '男'),
('202302305' , '三毛' , '男' ),
('202302306' , '柯南' , '男'),
('202302307' , '凌美琪' , '女'),
('202302308' , '凌美雪' , '女'),
('202302309' , '孙悟空' , '男'),
('202302310' , '猪八戒' , '男'),
('202302311' , '沙和尚' , '男'),
('202302312' , '唐僧' , '男'),
('202302313' , '林黛玉' , '女'),
('202302314' , '贾宝玉' , '男'),
('202302315' , '奥特曼' , '男'),
('202302316' , '汤姆' , '男'),
('202302317' , '杰瑞' , '男'),
('202302318' , '朵拉' , '女'),
('202302319' , '一休' , '男'),
('202302320' , '米老鼠' , '男'),
('202302321' , '唐老鸭' , '男'),
('202302322' , '哆啦A梦' , '男'),
('202302323' , '成龙' , '男'),
('202302324' , '小玉' , '女'),
('202302325' , '加菲猫' , '男'),
('202302326' , '喜羊羊' , '男'),
('202302327' , '灰太狼' , '男'),
('202302328' , '虹猫' , '男'),
('202302329' , '蓝兔' , '女'),
('202302330' , '洛洛' , '男');
Are there any characters you like here❓❓❓
Yes, I like it too
┅
According to my understanding, I will divide it into two parts to explain
Table of contents
Part 1: Query the specified paging statement
Part 2: Query fixed paging statements
Part 1: Query the specified paging statement
①limit takes one parameter
Syntax: limit the number of records displayed on each page;
Note: This writing method is only used when only querying the first page of data.
Case explanation
Requirement 1: Query the records of the first 5 rows on the first page;
Analysis: Query the records on the first page and display 5 pieces of data. Just write 5 .
SQL writing:
select * from tb_students limit 5;
The query results are as follows:
Note: You can also bring two parameters, written as limit 0, 5, which has the same effect as limit 5; both methods can be used;
②limit takes two parameters
Syntax: limit starting index, number of records displayed on each page;
Formula for calculating starting index:
start index = start index - 1
Note: The starting indexes all start from 0 (but do not include 0).
Case explanation
Requirement: Query the record of row 7 and display 2 rows of records;
Analysis:
limit first parameter (starting index):
Apply the formula for calculating the starting index: starting index = starting index - 1;
First of all, what I want to query is the record of row 7, then my starting index is 7, that is 7 - 1 = 6 , and finally my starting index is: 6 ;
limit second parameter (number of records displayed per page):
I want to display 2 rows of records on this page. In the end, the number of records displayed on each page is: 2 .
SQL writing:
select * from tb_students limit 6, 2;
The query results are as follows:
Another way to write it:
Syntax: limit number of records displayed on each page offset starting index;
Formula for calculating starting index:
start index = start index - 1
The usage is the same as limit with two parameters; the only difference is in syntax: the order of the starting index and the number of records displayed on each page has been changed, and the "," sign has been replaced by offset.
SQL writing:
select * from tb_students limit 2 offset 6;
Get the same execution result:
Part 2: Query fixed paging statements
Syntax: limit starting index, number of records displayed on each page;
Formula for calculating starting index:
Starting index = (query page number - 1) * number of records displayed on each page
Case explanation
Requirements: Fixed display of 5 records per page, looking for 1 - 3 pages of records;
Page ①
Analysis:
limit first parameter (starting index):
Apply the formula for calculating the starting index: starting index = (query page number - 1) * number of records displayed on each page
Because we are querying the data on the first page, the query page number is 1, and then 1 goes to - 1, and we get: (1 - 1) = 0 ;
Then * display the number of records on each page. Each page should display 5 records, and we get: 0 * 5 = 0. Finally, my starting index is: 0 ;
limit second parameter (number of records displayed per page):
I want to display 5 rows of records on this page. In the end, the number of records displayed on each page is: 5 .
SQL writing:
select * from tb_students limit 0,5;-- 显示 1 - 5 行记录
The query results are as follows:
Page ②
Analysis:
limit first parameter (starting index):
Apply the formula for calculating the starting index: starting index = (query page number - 1) * number of records displayed on each page
Because we are querying the data on the first page, the query page number is 2, and then 2 also goes to - 1, and we get: (2 - 1) = 1 ;
Then * display the number of records on each page. Each page should display 5 records, and we get: 1 * 5 = 0. Finally, my starting index is: 5 ;
limit second parameter (number of records displayed per page):
I want to display 5 rows of records on this page. In the end, the number of records displayed on each page is: 5 .
SQL writing:
select * from tb_students limit 5,5;-- 显示 6 - 10 行记录
The query results are as follows:
Page ③
Analysis:
limit first parameter (starting index):
Apply the formula for calculating the starting index: starting index = (query page number - 1) * number of records displayed on each page
Because we are querying the data on the first page, the query page number is 3, and then 3 also goes to - 1, and we get: (3 - 1) = 2 ;
Then * display the number of records on each page. Each page should display 5 records, and we get: 2 * 5 = 10. Finally, my starting index is: 10 ;
limit second parameter (number of records displayed per page):
I want to display 5 rows of records on this page. In the end, the number of records displayed on each page is: 5 .
SQL writing:
select * from tb_students limit 10,5;-- 显示 11 - 15 行记录
The query results are as follows:
Full SQL:
select * from tb_students limit 0,5;-- 显示 1 - 5 行记录
select * from tb_students limit 5,5;-- 显示 6 - 10 行记录
select * from tb_students limit 10,5;-- 显示 11 - 15 行记录
If you want to query the data on the fourth page, the fifth page, the sixth page..., follow this formula, and so on. . .
If you have learned it, let’s practice it! ! !
Requirements: Fixed display of 10 records per page, looking for 1 - 3 pages of records;
select * from tb_students limit ?;
select * from tb_students limit ?;
select * from tb_students limit ?;
See the answer after you finish:
✿
✿
✿
✿
✿
✿
✿
✿
✿
✿
select * from tb_students limit 0,10;-- 显示 1 - 10 行记录
select * from tb_students limit 10,10;-- 显示 11 - 20 行记录
select * from tb_students limit 20,10;-- 显示 20 - 30 行记录
over. . .