在mysql 5.7中选择具有最大日期的不同行 [英] Select distinct rows with max date in mysql 5.7

查看:30
本文介绍了在mysql 5.7中选择具有最大日期的不同行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设有查询

SELECT c_id, id, max(date) as max_date FROM table
GROUP BY c_id,updated

结果如下:

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

我希望得到:

c_id, id, max_date 
1     6   2019-12-28 16:09:20
2     8   2019-12-28 16:09:20

如何在 mysql 5.7 中实现?

How to achieve that in mysql 5.7?

推荐答案

使用相关子查询:

select t.*
from t
where t.date = (select max(t2.date) from t t2 where t2.c_id = t.c_id);

这篇关于在mysql 5.7中选择具有最大日期的不同行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆