MySQL aggregate function and limit

Source: Internet
Author: User

In doing a polling project, there is a function that requires the number of votes to be detected before the bottom 10.

The following SQL is used here:

SELECT MIN  from vote_candidate WHERE > 0 GROUP  by  ORDERby DESC

After the completion of the question of the cardiac, in the End Is min () first execution, or limit first effective?

According to the previous understanding, the priority of limit is in the where, GROUP by, and ORDER by, is to limit the result set of the last return, presumably it is executed after the aggregation function, then this limit clause is meaningless, the resulting is not the expected result.

In practice, some test data are added to the table. An unexpected conclusion was drawn through the test:

The precedence of aggregation functions when they coexist with the limit clause is divided into two cases:

1. There is a GROUP BY clause (in this case): WHERE > Group by > ORDER by > LIMIT > Aggregation

2. No GROUP BY clause: WHERE > Aggregation > ORDER by > LIMIT

MySQL aggregate function and limit

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.