Reference: https://www.cnblogs.com/java-spring/p/11498457.html
During development, I encountered the need to query the latest piece of data in a group. The record is as follows:
Requirement: Select the latest piece of data in the consumer_app_name group from the table statistics.
Idea: Sort first, then group, and then ensure that the subquery is still in order.
- 1. First sort statistics in descending order
Note that the principle of using limit will be discussed later.
SELECT *
FROM statistics
order by create_date desc
limit 10000
- 2. Select the latest one from the list in descending order
The limit is to make the subqueries implemented by group by still in order.
select *
from(
SELECT *
FROM statistics
order by create_date desc
limit 10000
) as temp
group by consumer_app_name