Home > Database > Mysql Tutorial > mysql limit usage

mysql limit usage

hzc
Release: 2020-07-01 16:24:44
Original
5252 people have browsed it

The limit in mysql can be used to force the SELECT statement to return the specified number of records. LIMIT accepts one or two numeric parameters. 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.

mysql limit usage

#When we use query statements, we often need to return the first few or middle rows of data. What should we do at this time? Don't worry, mysql has provided us with such a function

SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset
Copy after login

The 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 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   
  
//为了检索从某一个偏移量到记录集的结束所有的记录行,可以指定第二个参数为 -1:    
mysql> SELECT * FROM table LIMIT 95,-1; // 检索记录行 96-last.   
  
//如果只给定一个参数,它表示返回最大的记录行数目:    
mysql> SELECT * FROM table LIMIT 5; //检索前 5 个记录行   
  
//换句话说,LIMIT n 等价于 LIMIT 0,n。
Copy after login
mysql> SELECT * FROM table LIMIT 5,10; // 检索记录行 6-15
Copy after login
//为了检索从某一个偏移量到记录集的结束所有的记录行,可以指定第二个参数为 -1: 
mysql> SELECT * FROM table LIMIT 95,-1; // 检索记录行 96-last.

//如果只给定一个参数,它表示返回最大的记录行数目: 
mysql> SELECT * FROM table LIMIT 5; //检索前 5 个记录行

//换句话说,LIMIT n 等价于 LIMIT 0,n。
Copy after login

Recommended tutorial: "mysql tutorial"

The above is the detailed content of mysql limit usage. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.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