当前位置:网站首页>[MySQL] limit implements paging
[MySQL] limit implements paging
2022-07-06 08:47:00 【Charming】
List of articles
Pagination
1. background :
background 1: The query returned too many records , It's inconvenient to check , How to implement paging query ?
background 2: There are 4 Data , If you only want to display 2、3 What about this data ?
2. Implementation rules
Pagination principle
Pagination display , The result set in the database , Show the required conditions paragraph by paragraph .MySQL Use in LIMIT Implement paging
Format :
LIMIT [ Position offset ,] Row numberfirst “ Position offset ” Parameters indicate MySQL Which line to start with , Is an optional parameter , If you don't specify “ Position offset ”, It will start with the first record in the table ( The position offset of the first record is
0, The position offset of the second record is1, And so on ); The second parameter “ Row number ” Indicates the number of records returned .give an example
-- front 10 Bar record :
SELECT * FROM Table name LIMIT 0,10;
perhaps
SELECT * FROM Table name LIMIT 10;
-- The first 11 to 20 Bar record :
SELECT * FROM Table name LIMIT 10,10;
-- The first 21 to 30 Bar record :
SELECT * FROM Table name LIMIT 20,10;
Use limit Realize pagination display of data
demand 1: Each page shows 5 Bar record , The... Is displayed 1 page
SELECT employee_id,last_name
FROM employees
LIMIT 0,5;

- demand 2: Each page shows 6 Bar record , The... Is displayed 2 page
SELECT employee_id,last_name
FROM employees
LIMIT 5,6;

- demand 3: Each page shows 7 Bar record , The... Is displayed 3 page
SELECT employee_id,last_name
FROM employees
LIMIT 6,7;

- demand 4: Each page shows
pageSizeBar record , The... Is displayedpageNopage : - The formula :
LIMIT (pageNo-1) * pageSize, pageSize;
- Paging explicit formula :
( The current number of pages -1)* Number of entries per page , Number of entries per page
SELECT * FROM table
LIMIT(PageNo - 1)*PageSize, PageSize;
Be careful :LIMIT Clause must be placed throughout SELECT At the end of the sentence !
Use
LIMITThe benefits of :
The number of results returned by the constraint canReduce the network transmission of data tables, It's fine tooImprove query efficiency. If we know that the return result is only 1 strip , You can useLIMIT 1, tell SELECT Statement only needs to return a record . The good thing is SELECT There is no need to scan the complete table , You only need to retrieve a qualified record to return .WHERE ... ORDER BY ...LIMITThe order of declaration is as follows :LIMIT The format of : Strictly speaking :
LIMIT Position offset , Number of entriesstructure "
LIMIT 0, Number of entries" Equivalent to “LIMIT Number of entries”
SELECT employee_id,last_name,salary
FROM employees
WHERE salary > 6000
ORDER BY salary DESC
#limit 0,10;
LIMIT 10;

- practice : There are 107 Data , If you only want to display 32、33 What about this data ?
SELECT employee_id,last_name
FROM employees
LIMIT 31,2;

MySQL 8.0 Can be used in “LIMIT 3 OFFSET 4”, It means to get from 5 A record starts after 3 Bar record , and “LIMIT 4,3;” The results returned are the same .
MySQL8.0 New characteristics :
LIMIT ... OFFSET ...practice : There are 107 Data , If you only want to display 32、33 What about this data ?
SELECT employee_id,last_name
FROM employees
LIMIT 2 OFFSET 31;

- practice : Query the information of the highest paid employee in the employee table
SELECT employee_id,last_name,salary
FROM employees
ORDER BY salary DESC
#limit 0,1
LIMIT 1;

3. expand
LIMIT It can be used in MySQL、PGSQL、MariaDB、SQLite And so on , Represents paging . Cannot be used in SQL Server、DB2、Oracle in .
In different DBMS The keywords used in may be different . stay MySQL、PostgreSQL、MariaDB and SQLite Use in LIMIT keyword , And it needs to be put in SELECT At the end of the statement .
If it is SQL Server and Access, Need to use
TOPkeyword , such as :
SELECT TOP 5 last_name, employee_id
FROM employees
ORDER BY employee_id DESC
- If it is DB2, Use
FETCH FIRST 5 ROWS ONLYSuch keywords :
SELECT last_name, employee_id
FROM employees
ORDER BY employee_id DESC
FETCH FIRST 5 ROWS ONLY
- If it is Oracle, Need to be based on
ROWNUMTo count the number of lines :
Such as : Before query 10 Bar record
SELECT rownum, employee_id, last_name
FROM employees
where rownum<= 10;

SELECT rownum,last_name,salary
FROM employees
WHERE rownum <= 5 ORDER BY salary DESC;
4. practice
- Check the employee's name, department number and annual salary , In descending order of annual salary , Display by name in ascending order
SELECT last_name,department_id,salary * 12 annual_salary
FROM employees
ORDER BY annual_salary DESC,last_name ASC;

- The choice of salary is not in 8000 To 17000 The name and salary of the employee , In descending order of wages , According to the first 21 To 40 Location data
SELECT last_name,salary
FROM employees
WHERE salary NOT BETWEEN 8000 AND 17000
ORDER BY salary DESC
LIMIT 20,20;

- Query mailbox contains e Employee information , And first by the number of bytes in the mailbox , And then in ascending order by department number
SELECT employee_id,last_name,email,department_id
FROM employees
#where email like '%e%'
WHERE email REGEXP '[e]'
ORDER BY LENGTH(email) DESC,department_id;

边栏推荐
- LeetCode:214. 最短回文串
- Sublime text in CONDA environment plt Show cannot pop up the problem of displaying pictures
- How to conduct interface test? What are the precautions? Nanny level interpretation
- Roguelike game into crack the hardest hit areas, how to break the bureau?
- 生成器参数传入参数
- JVM quick start
- Crash problem of Chrome browser
- 如何进行接口测试测?有哪些注意事项?保姆级解读
- China's high purity aluminum target market status and investment forecast report (2022 Edition)
- To effectively improve the quality of software products, find a third-party software evaluation organization
猜你喜欢

TCP/IP协议

【嵌入式】使用JLINK RTT打印log

Roguelike game into crack the hardest hit areas, how to break the bureau?

Deep analysis of C language data storage in memory

Problems in loading and saving pytorch trained models

Marathon envs project environment configuration (strengthen learning and imitate reference actions)

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

TP-LINK enterprise router PPTP configuration

C语言深度解剖——C语言关键字

Computer cleaning, deleted system files
随机推荐
pcd转ply后在meshlab无法打开,提示 Error details: Unespected eof
Guangzhou will promote the construction of a child friendly city, and will explore the establishment of a safe area 200 meters around the school
Bitwise logical operator
Excellent software testers have these abilities
LeetCode:34. 在排序数组中查找元素的第一个和最后一个位置
力扣每日一题(二)
有效提高软件产品质量,就找第三方软件测评机构
JS pure function
hutool优雅解析URL链接并获取参数
Revit secondary development Hof method calls transaction
LeetCode:124. 二叉树中的最大路径和
【ROS】usb_ Cam camera calibration
按位逻辑运算符
Roguelike game into crack the hardest hit areas, how to break the bureau?
sublime text的编写程序时的Tab和空格缩进问题
Esp8266-rtos IOT development
【嵌入式】Cortex M4F DSP库
[embedded] print log using JLINK RTT
LeetCode:剑指 Offer 48. 最长不含重复字符的子字符串
R language uses the principal function of psych package to perform principal component analysis on the specified data set. PCA performs data dimensionality reduction (input as correlation matrix), cus