The magic of limit within MySQL

Source: Internet
Author: User

In the project there is a need to query a table, group by, and then the group's count (*) in reverse order, take out each group inside the latest record of the publication time, the former colleague SQL wrote this

SELECT    * from    (        SELECT            *         from            Table        WHEREID= ?  andStatus= ? ORDER  byCreated_timeDESC    )  asaGROUP  byA.GroupORDER  by    Count(*)DESCLIMITTen

However, the final data is out of order, that is, the data in the group is not the latest in the publication time, very strange. Finally, I added a limit to this statement, as follows

SELECT    * from    (        SELECT            *         from            Table        WHEREID= ?  andStatus= ? ORDER  byCreated_timeDESCLIMIT10000    )  asaGROUP  byA.GroupORDER  by    Count(*)DESCLIMITTen

Although the limit is 10000, but the entire table is actually more than 3,000 data, but so that the data found in the group is the latest data. I was thinking that the limit in MySQL is not like Java's volatile, with the code optimization to keep the variable relative position "magic"

The magic of limit within MySQL

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.