There is no top query in MySQL, but you can use limit query to achieve the same effect. The syntax is "SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset".
Friends who are used to access mssql server may be accustomed to using the select top n form of statements when querying the first N records with mysql, here To explain, mysql does not have this syntax. Mysql uses limit to implement related functions, and the function is more powerful, GOOD. The following is a detailed explanation of the use of limit in mysql:
Syntax:
SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset
The LIMIT clause can be used to force the SELECT statement to return a 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; //Retrieve record rows 6-15, note that 10 is the offset
//In order to retrieve all rows from a certain offset to the end of the record set Record row, you can 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 Indicates returning 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.
If you want to get the last few pieces of data, you can add an additional order by id desc
mysql does not support the syntax of select top n and should be replaced with this:
select * from tablename order by orderfield desc/asc limit position, counter;
position indicates where to start querying, if it is 0, it starts from the beginning, counter indicates the number of queries
Get the first 15 records:
select * from tablename order by orderfield desc/asc limit 0,15
The above is the detailed content of Does mysql have top query?. For more information, please follow other related articles on the PHP Chinese website!