It is not difficult to implement MySQL top n Query

Source: Internet
Author: User

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:

 
 
  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 the description of MySQL top n query, hoping to help you in this regard.

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.