我有下面提到的表格:
ID Date Value
AL-1 2017-04-01 22:04:08 154
AL-1 2017-04-05 15:08:45 158
AL-1 2017-04-06 18:09:15 225
AL-1 2017-04-08 20:08:17 254
AL-2 2017-04-01 22:04:08 154
我正在尝试:从表 1 中选择 ID、值,其中 id 在 ('AL-1','AL-2') 中并按 max(date(Date)); 分组;
但它给我这样的输出:
ID Date Value
AL-1 2017-04-01 22:04:08 154
AL-2 2017-04-01 22:04:08 154
期望的输出:
ID Date Value
AL-1 2017-04-08 20:08:17 254
AL-2 2017-04-01 22:04:08 154
最佳答案
如果您想为每个 ID
获取最新的行,您可以使用 correlated subquery
:
select t1.*
from table1 t1
where Date = (select max(t2.Date) from tabel1 t2 where t1.ID = t2.ID);
关于mysql - 如何从 Mysql 中获取最新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50253338/