当前位置:网站首页>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)
原网站

版权声明
本文为[Rsda DBA_ WGX]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207230648567003.html