Home  >  Article  >  Database  >  How to set the number of displayed items in mysql query?

How to set the number of displayed items in mysql query?

青灯夜游
青灯夜游Original
2020-09-28 09:46:385511browse

Mysql query setting method to display the number of records: Use the LIMT clause to force the SELECT statement to return the specified number of records, the syntax is "SELECT * FROM table name LIMIT [position offset,] number of rows"; LIMT parameter Must be an integer constant, where the "position offset" specifies which row to start displaying.

How to set the number of displayed items in mysql query?

(Recommended tutorial: mysql video tutorial)

When using the MySQL SELECT statement, all matches are often returned rows. Sometimes we only need to return the first row or the first few rows. At this time, we need to use the MySQL LIMT clause.

The basic syntax format is as follows:

<LIMIT> [<位置偏移量>,] <行数>

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.

The first parameter "Position offset" indicates which row MySQL starts to display. It is an optional parameter. If the "position offset" is not specified, it will be displayed from the table. Starting from the first record in (the position offset of the first record is 0, the position offset of the second record is 1, and so on); the second parameter "number of rows" indicates the returned record strip number.

[Example 1] Display the first 4 rows of the query results of the tb_students_info table. The input SQL statement and execution results are as follows.

How to set the number of displayed items in mysql query?

If the "position offset" is not specified in the above query conditions, the query will start from the first record by default, and the result will display 4 query records;

If you specify the starting position of the returned record, the returned result is the specified number of rows starting from the "position offset" parameter, and the "number of rows" parameter specifies the number of records to be returned.

[Example 2] In the tb_students_info table, use the LIMIT clause to return 5 records starting from the 4th record. The input SQL statement and execution results are as follows.

How to set the number of displayed items in mysql query?

As you can see from the results, this statement instructs MySQL to return the next 5 records starting from the 4th record row. The first number "3" means starting from the 4th record row. Starting at row 4 (the position offset starts from 0, the position offset of row 4 is 3), the second number 5 represents the number of rows returned.

So, LIMIT with one parameter specifies starting from the first row of the query result, and the only parameter indicates the number of rows returned, that is, "LIMIT n" is equivalent to "LIMIT 0, n". LIMIT with two arguments returns a specified number of rows of data starting at any position.

When returning to the first row, the position offset is 0. Therefore, "LIMIT 1,1" returns row 2, not row 1.

Note: "LIMIT 4 OFFSET 3" can be used in MySQL 5.7, which means to get the next 3 records starting from the 5th record, and "LIMIT 4, 3" returns The results are the same.

Related recommendations: php training

The above is the detailed content of How to set the number of displayed items in mysql query?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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