问题
Suppose having query
SELECT c_id, id, max(date) as max_date FROM table
GROUP BY c_id,updated
And following result:
c_id, id, max_date
1 5 2017-12-28 16:09:20
1 6 2019-12-28 16:09:20
2 7 2017-12-28 16:09:20
2 8 2019-12-28 16:09:20
I expect to get:
c_id, id, max_date
1 6 2019-12-28 16:09:20
2 8 2019-12-28 16:09:20
How to achieve that in mysql 5.7?
回答1:
Use a correlated subquery:
select t.*
from t
where t.date = (select max(t2.date) from t t2 where t2.c_id = t.c_id);
来源:https://stackoverflow.com/questions/63287914/select-distinct-rows-with-max-date-in-mysql-5-7