MySQL database - MySQL LIMIT: limit the number of query results

When there are tens of thousands of data in the data table, querying all the data in the table at one time will reduce the speed of data return and put a lot of pressure on the database server. At this time, you can use the LIMIT keyword to limit the number of records returned by the query results.

LIMIT is a special keyword in MySQL, which is used to specify from which record the query results are displayed and how many records are displayed in total.

There are 3 ways to use the LIMIT keyword, that is, to specify the initial position, not to specify the initial position, and to use it in combination with OFFSET.

specify initial position

The LIMIT keyword can specify which record to display the query results from and how many records to display.

The basic syntax format of LIMIT to specify the initial position is as follows:

LIMIT initial position, number of records

Among them, "initial position" indicates which record to start displaying; "number of records" indicates the number of displayed records. The position of the first record is 0 and the position of the second record is 1. Subsequent records are followed by analogy.

Note: Both parameters after LIMIT must be positive integers.

Example 1

In the tb_students_info table, use the LIMIT clause to return 5 records starting from the 4th record. The SQL statement and running results are as follows:

mysql> SELECT * FROM tb_students_info LIMIT 3,5;
+----+-------+---------+------+------+--------+------------+
| id | name  | dept_id | age  | sex  | height | login_date |
+----+-------+---------+------+------+--------+------------+
|  4 | Jane  |       1 |   22 | F    |    162 | 2016-12-20 |
|  5 | Jim   |       1 |   24 | M    |    175 | 2016-01-15 |
|  6 | John  |       2 |   21 | M    |    172 | 2015-11-11 |
|  7 | Lily  |       6 |   22 | F    |    165 | 2016-02-26 |
|  8 | Susan |       4 |   23 | F    |    170 | 2015-10-01 |
+----+-------+---------+------+------+--------+------------+
5 rows in set (0.00 sec)

As can be seen from the result, the statement returns the 5 records starting from the 4th record. The first number "3" after the LIMIT keyword means starting from the 4th line (the record position starts from 0, and the position of the 4th line is 3), and the second number 5 means the number of rows returned.

no initial position specified

When the LIMIT keyword does not specify an initial position, records are displayed from the first record. The number of displayed records is specified by the LIMIT keyword.

The basic syntax format of LIMIT without specifying the initial position is as follows:

LIMIT number of records

Wherein, "number of records" indicates the number of displayed records. 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.

Example 2

Display the first 4 rows of the query results of the tb_students_info table, the SQL statement and the running results are as follows:

mysql> SELECT * FROM tb_students_info LIMIT 4;
+----+-------+---------+------+------+--------+------------+
| id | name  | dept_id | age  | sex  | height | login_date |
+----+-------+---------+------+------+--------+------------+
|  1 | Dany  |       1 |   25 | F    |    160 | 2015-09-10 |
|  2 | Green |       3 |   23 | F    |    158 | 2016-10-22 |
|  3 | Henry |       2 |   23 | M    |    185 | 2015-05-31 |
|  4 | Jane  |       1 |   22 | F    |    162 | 2016-12-20 |
+----+-------+---------+------+------+--------+------------+
4 rows in set (0.00 sec)

Only 4 records are displayed in the result, indicating that "LIMIT 4" limits the number of displayed records to 4.

Example 3

Display the first 15 rows of the query results of the tb_students_info table, the SQL statement and the running results are as follows:

mysql> SELECT * FROM tb_students_info LIMIT 15;
+----+--------+---------+------+------+--------+------------+
| id | name   | dept_id | age  | sex  | height | login_date |
+----+--------+---------+------+------+--------+------------+
|  1 | Dany   |       1 |   25 | F    |    160 | 2015-09-10 |
|  2 | Green  |       3 |   23 | F    |    158 | 2016-10-22 |
|  3 | Henry  |       2 |   23 | M    |    185 | 2015-05-31 |
|  4 | Jane   |       1 |   22 | F    |    162 | 2016-12-20 |
|  5 | Jim    |       1 |   24 | M    |    175 | 2016-01-15 |
|  6 | John   |       2 |   21 | M    |    172 | 2015-11-11 |
|  7 | Lily   |       6 |   22 | F    |    165 | 2016-02-26 |
|  8 | Susan  |       4 |   23 | F    |    170 | 2015-10-01 |
|  9 | Thomas |       3 |   22 | M    |    178 | 2016-06-07 |
| 10 | Tom    |       4 |   23 | M    |    165 | 2016-08-05 |
+----+--------+---------+------+------+--------+------------+
10 rows in set (0.26 sec)

Only 10 records are displayed in the results. Although the LIMIT keyword specifies displaying 15 records, only 10 records are in the query results. Therefore, the database system displays all these 10 records.

LIMIT with one parameter specifies starting from the first row of the query result, and the only parameter indicates the number of rows returned, that is, "LIMIT n" returns the same result as "LIMIT 0, n". LIMIT with two parameters returns the data for the specified number of rows starting at any position.

Combination of LIMIT and OFFSET

LIMIT can be used in combination with OFFSET, the syntax is as follows:

LIMIT number of records OFFSET initial position

The parameter has the same meaning as the parameter in the LIMIT syntax, the "initial position" specifies which record to start displaying; the "record number" indicates the number of displayed records.

Example 4

In the tb_students_info table, use LIMIT OFFSET to return the records whose row number is 5 starting from the fourth record. The SQL statement and the running results are as follows.

mysql> SELECT * FROM tb_students_info LIMIT 5 OFFSET 3;
+----+-------+---------+------+------+--------+------------+
| id | name  | dept_id | age  | sex  | height | login_date |
+----+-------+---------+------+------+--------+------------+
|  4 | Jane  |       1 |   22 | F    |    162 | 2016-12-20 |
|  5 | Jim   |       1 |   24 | M    |    175 | 2016-01-15 |
|  6 | John  |       2 |   21 | M    |    172 | 2015-11-11 |
|  7 | Lily  |       6 |   22 | F    |    165 | 2016-02-26 |
|  8 | Susan |       4 |   23 | F    |    170 | 2015-10-01 |
+----+-------+---------+------+------+--------+------------+
5 rows in set (0.00 sec)

As can be seen from the result, the statement returns the 5 records starting from the 4th record. That is, "LIMIT 5 OFFSET 3" means to get the next 5 records starting from the 4th record, and the result returned by "LIMIT 3, 5" is the same.

Dark horse programmer MySQL database entry to proficiency, from mysql installation to mysql advanced, mysql optimization all covered

Guess you like

Origin blog.csdn.net/Itmastergo/article/details/130336396