This article mainly describes the actual operations of MySQL top n queries. We all know that the proportion of MySQL top n queries in actual applications is still relatively large. If you understand the actual operations, this will make your actual operations more convenient.
We know that in ms SQL server or access,
To query the first 10 records, use the top 10 records,
However, MySQL does not support this method. It uses limit 10.
We can use a sub-Statement supported by SELECT in MySQL -- LIMIT -- to complete this function.
LIMIT can be used to query the top N or a segment between M and N. The specific syntax is as follows:
- SELECT * FROM MYTABLE
- ORDER BY AFIELD
- LIMIT offset, recnum
Here, offset is the number of M + 1 records starting from, and recnum is the number of returned records. Example:
- Select * from mytable
- Order by afield
- Limit 2, 5
That is, five records starting from 3rd records.
The above content is the description of MySQL top n query, hoping to help you in this regard.