How to Implement MySQL top n Query

Source: Internet
Author: User

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:

 
 
  1. SELECT * FROM MYTABLE
  2. ORDER BY AFIELD
  3. LIMIT offset, recnum

Here, offset is the number of M + 1 records starting from, and recnum is the number of returned records. Example:

 
 
  1. Select * from mytable
  2. Order by afield
  3. 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.