20. LIMIT in MySQL (pagination)

For a large number of records retrieved at one time, it is not only inconvenient to read and view, but also wastes system efficiency. MySQL provides a keyword LIMIT , you can limit the number of records , you can also specify the query from which record (usually used for paging).

1. Prepare

 1 CREATE DATABASE mahaiwuji;
 2 USE mahaiwuji;
 3 
 4 CREATE TABLE stu (
 5   id int(10),
 6   name varchar(20),
 7   sex int(5),
 8   PRIMARY KEY (id)
 9 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
10  
11 INSERT INTO stu VALUES ('1', 'aa', '1');
12 INSERT INTO stu VALUES ('2', 'bb', '1');
13 INSERT INTO stu VALUES ('3', 'cc', '2');
14 INSERT INTO stu VALUES ('4', 'dd', '2');
15 INSERT INTO stu VALUES ('5', 'ee', '1');
16 INSERT INTO stu VALUES ('6', 'ff', '1');
17 INSERT INTO stu VALUES ('7', 'gg', '2');
18 INSERT INTO stu VALUES ('8', 'hh', '2');
19 INSERT INTO stu VALUES ('9', 'ii', '1');
20 INSERT INTO stu VALUES ('10', 'jj', '1');
21 INSERT INTO stu VALUES ('11', 'kk', '2');
22 INSERT INTO stu VALUES ('12', 'll', '2');
23 INSERT INTO stu VALUES ('13', 'mm', '1');
24 INSERT INTO stu VALUES ('14', 'nn', '1');

2. One parameter

When there is only one limit parameter , the meaning of this parameter is to query the data whose subscript is less than this parameter , which can be understood as the first few .

1 SELECT * FROM stu LIMIT 3;

1 SELECT * FROM stu LIMIT 5;

3. Two parameters

When the limit parameter is two , the meaning of the first parameter is to display the data with subscripts starting from the parameter , and the meaning of the second parameter is how many to display .

1 SELECT * FROM stu LIMIT 3,5;

1 SELECT * FROM stu LIMIT 6,3;

1  - When the number of entries is not enough, only the data that meets the conditions will be displayed 
2  SELECT  *  FROM stu LIMIT 10 , 5 ;

Guess you like

Origin www.cnblogs.com/mahaiwuji/p/12683627.html