The following articles mainly introduce the actual operation steps of MySQL top n query. We all know that in ms SQL server or in access, if you want to query the first 10 records, the top 10 task can be completed, but 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 MySQL top N records, or MB to N records. 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 an introduction to the implementation of MySQL top N queries. I hope you will have some gains.