Detailed explanation of the usage of limit in Mysql

The usage of limit in Mysql: When we use query statements, we often have to return the first few or some rows of data in the middle. 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

 

  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 argument must be an integer constant. If two parameters are given, the first parameter specifies the offset of the first returned record line, and the second parameter specifies the maximum number of returned record lines. 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; // retrieve records rows 6-15

 

  //To retrieve all rows from an offset to the end of the recordset, specify the second parameter as -1:

 

  mysql> SELECT * FROM table LIMIT 95,-1; // Retrieve record row 96-last.

 

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

 

  mysql> SELECT * FROM table LIMIT 5; //Retrieve the first 5 record rows

 

  // In other words, LIMIT n is equivalent to LIMIT 0,n.

 

  

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326873553&siteId=291194637