Home >Database >Mysql Tutorial >How to use limit Mysql query statement
In my opinion, limit should be the most frequently used method in mysql. Let’s explain the function of limit and how to use this method correctly.
SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset
The LIMIT clause can be used to force the SELECT statement to return a specified number of records. LIMIT accepts one or two numeric arguments. The parameter 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; // 检索记录行 6-15
// In order to retrieve all record lines from a certain offset to the end of the recordset, you can specify the second parameter as -1:
mysql> SELECT * FROM table LIMIT 95,-1; // 检索记录行 96-last.
// If only one parameter is given, it means returning the maximum number of record rows:
mysql> SELECT * FROM table LIMIT 5; //检索前 5 个记录行
//In other words, LIMIT n is equivalent to LIMIT 0,n.