Select distinct rows with max date in mysql 5.7 [duplicate]

浪子不回头ぞ 提交于 2021-02-20 03:49:17

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!