当前位置:网站首页>Tutorial on the principle and application of database system (044) -- MySQL query (VI): using the limit option to realize paging query
Tutorial on the principle and application of database system (044) -- MySQL query (VI): using the limit option to realize paging query
2022-07-24 00:49:00 【Rsda DBA_ WGX】
Database system principle and Application Tutorial (044)—— MySQL Inquire about ( 6、 ... and ): Use LIMIT Option to realize paging query
Use in query LIMIT Option can return the data of the first few rows or some middle rows .
One 、LIMIT Syntax of options
LIMIT The syntax format of options is as follows :
LIMIT [offsets,] rows
-- perhaps
LIMIT rows OFFSET offsets
/* explain : (1) This option is often associated with order by Use it together ; (2)LIMIT You need to specify one or two integer parameters ; (3) If you specify two parameters , The first parameter specifies the number of records (offset+1) Start calculating , The second parameter specifies the maximum number of rows to return records ; (3) If only one parameter is specified ( The first parameter defaults to 0), This parameter is calculated from the first record , Maximum number of rows to return . */
Two 、 Specify only one parameter
for example : Search for courses C102 The highest score 3 Bar record .
mysql> select * from score where c_id = 'C102'
order by score desc limit 3;
+-------+------+-------+
| s_id | c_id | score |
+-------+------+-------+
| S2013 | C102 | 97 |
| S2014 | C102 | 90 |
| S2022 | C102 | 88 |
+-------+------+-------+
3 rows in set (0.00 sec)
3、 ... and 、 Specify two parameters
for example : Search for courses C102 achievement 4-6 A record of names .
mysql> select * from score where c_id = 'C102'
order by score desc limit 3,3;
+-------+------+-------+
| s_id | c_id | score |
+-------+------+-------+
| S2024 | C102 | 87 |
| S2011 | C102 | 84 |
| S2021 | C102 | 72 |
+-------+------+-------+
3 rows in set (0.00 sec)
Four 、 Use rows OFFSET offsets Format
for example : Search for courses C102 achievement 4-6 A record of names .
mysql> select * from score where c_id = 'C102'
-> order by score desc limit 3 offset 3;
+-------+------+-------+
| s_id | c_id | score |
+-------+------+-------+
| S2024 | C102 | 87 |
| S2011 | C102 | 84 |
| S2021 | C102 | 72 |
+-------+------+-------+
3 rows in set (0.00 sec)
5、 ... and 、 utilize LIMIT Option to realize paging query
(1) Create table t11 And input data
/* create table t11(id int primary key auto_increment,name char(20),salary); insert into t11(name, salary) values(concat(' king ',round(rand()*100000,0)),round(10000+10000*rand(),0)); */
mysql> create table t11(id int primary key auto_increment,name char(20),salary int);
Query OK, 0 rows affected (0.03 sec)
mysql> insert into t11(name, salary) values(concat(' king ',round(rand()*100000,0)),round(10000+10000*rand(),0));
Query OK, 1 row affected (0.04 sec)
--insert into t11(name,salary) select concat(' king ',round(rand()*100000,0)),round(10000+10000*rand(),0)) from t11;
mysql> select count(*) from t11;
+----------+
| count(*) |
+----------+
| 5632 |
+----------+
1 row in set (0.01 sec)
(2) Each page shows 10 Bar record
-- The first 1 page
mysql> select * from t11 limit 0,10;
+----+----------+--------+
| id | name | salary |
+----+----------+--------+
| 1 | king 69892 | 19234 |
| 2 | king 52000 | 18300 |
| 3 | king 58978 | 14590 |
| 4 | king 52587 | 12522 |
| 5 | king 68343 | 16605 |
| 6 | king 25239 | 12803 |
| 7 | king 64457 | 13818 |
| 8 | king 97521 | 17307 |
| 9 | king 72786 | 14472 |
| 10 | king 5248 | 19208 |
+----+----------+--------+
10 rows in set (0.00 sec)
-- The first 2 page
mysql> select * from t11 limit 10,10;
+----+----------+--------+
| id | name | salary |
+----+----------+--------+
| 11 | king 44640 | 14697 |
| 12 | king 928 | 16373 |
| 13 | king 15874 | 18818 |
| 14 | king 93259 | 10176 |
| 15 | king 29047 | 13994 |
| 16 | king 12568 | 14302 |
| 17 | king 77375 | 15783 |
| 18 | king 57015 | 11159 |
| 19 | king 86907 | 19976 |
| 20 | king 38096 | 19119 |
+----+----------+--------+
10 rows in set (0.00 sec)
-- The first 3 page
mysql> select * from t11 limit 20,10;
+----+----------+--------+
| id | name | salary |
+----+----------+--------+
| 21 | king 41671 | 13478 |
| 22 | king 48877 | 14005 |
| 27 | king 53629 | 14799 |
| 28 | king 79065 | 15135 |
| 29 | king 19568 | 14378 |
| 30 | king 60202 | 16967 |
| 31 | king 67737 | 12968 |
| 32 | king 45187 | 13689 |
| 33 | king 48914 | 13389 |
| 34 | king 22692 | 11180 |
+----+----------+--------+
10 rows in set (0.01 sec)
边栏推荐
- Classic example of C language - loan balance
- MySQL exercise: all employees reporting to the CEO
- Summary of pit websocket
- Detailed overview of data standards -2022
- First knowledge of C language functions
- Analysis of the advantages of the LAAS scheme of elephant swap led to strong performance of ETOKEN
- Résumé du websocket minier
- 北峰通信亮相中国(厦门)应急展|智能化通信手段强势吸睛!
- 落枕如何快速缓解
- 【LeetCode第 83 场双周赛】
猜你喜欢

Customize an object

Treatment of particle boundary collision

High number_ Chapter 2 differential calculus of multivariate functions__ Geometric application of partial derivatives_ Tangent and normal plane of space curve

Pbootcms database conversion tutorial (SQLite to MySQL detailed tutorial)

Classic example of C language - convert the input two digits into English

Generic mechanism and enhanced for loop

如何在自动化测试中使用MitmProxy获取数据返回?

Database connection pool & dbutils

Detailed explanation of data warehouse standard -2022

测试小码农也有大目标,最新BAT大厂面试题大总结(持续更新中...)
随机推荐
Leetcode set the intersection size to at least 2
Résumé du websocket minier
Beifeng communication appeared in China (Xiamen) emergency exhibition | intelligent communication means are strong and eye-catching!
MySQL client to server character set conversion
T-seda code
PostgreSQL snapshot optimization globalvis new system analysis (greatly enhanced performance)
How to improve data quality
Application of SCA on devsecops platform
PayPal subscription process and API request
Printf function - conversion description
落枕如何快速缓解
【LeetCode第 83 场双周赛】
An article teaches you the basic use of kubernetes
暑假第四周总结
The most basic code interpretation of C language
[video game training] non contact object size and shape measurement 2020 video game G
Redis cluster hash sharding algorithm (slot location algorithm)
Classic examples of C language - adding two scores
Understanding polymorphism and letting different "people" do the same thing will produce different results
Flutter | specifies the type of page return value