Home > Database > Mysql Tutorial > body text

Must learn! How to use limit in MySQL database query

silencement
Release: 2020-01-23 22:48:35
forward
3219 people have browsed it

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
Copy after login

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
Copy after login

//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.
Copy after login

//If only one parameter is given, it means returning the maximum number of record rows:

mysql> SELECT * FROM table LIMIT 5; //检索前 5 个记录行
Copy after login

//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!

Related labels:
source:www.liqingbo.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template