Home > Article > Backend Development > Detailed explanation and precautions for usage of limit in Mysql_PHP tutorial
When we use query statements, we often need to return the first few rows of data or the middle rows of data. What should we do at this time? Don't worry,
mysql already provides us with such a function.
SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset
LIMIT clause can be used to force the SELECT statement to return the specified number of records. LIMIT accepts one or two numeric arguments. The argument must
be an integer constant. If two parameters are given, the first parameter specifies the offset of the first returned record row, and the second parameter specifies the maximum number of returned
record rows. The offset of the initial record row is 0 (instead of 1): For compatibility with PostgreSQL, MySQL also supports the syntax: LIMIT # OFFSET #.
mysql> SELECT * FROM table LIMIT 5,10; //Retrieve record rows 6-15
//In order to retrieve all records from a certain offset to the end of the record set For record lines, you can specify the second parameter as -1:
mysql> SELECT * FROM table LIMIT 95,-1; // Retrieve record lines 96-last.
//If only given A parameter that represents the maximum number of record rows returned:
mysql> SELECT * FROM table LIMIT 5; //Retrieve the first 5 record rows
//In other words, LIMIT n is equivalent to LIMIT 0,n.