Home  >  Article  >  Database  >  Must learn! How to use limit in MySQL database query

Must learn! How to use limit in MySQL database query

silencement
silencementforward
2020-01-23 22:48:353067browse

Must learn! How to use limit in MySQL database query

In my opinion, limit should be the most commonly used method in mysql. Let's explain the role 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 a SELECT statement to return a specified number of records. LIMIT accepts one or two numeric arguments. The parameter must be an integer constant. If

is given two parameters, 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.

The above is the detailed content of Must learn! How to use limit in MySQL database query. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:www.liqingbo.cn. If there is any infringement, please contact admin@php.cn delete