我有下面提到的表格:
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我尝试这样做:select ID, Value from table1 where id in ('AL-1','AL-2') and group by 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发布于 2018-05-09 20:37:05
如果要读取每个ID的最新行,则可以使用相关subquery
select t1.*
from table1 t1
where Date = (select max(t2.Date) from tabel1 t2 where t1.ID = t2.ID);发布于 2018-05-09 20:38:41
有一个子查询,它返回每个id及其最大日期。具有该结果的JOIN:
select t1.ID, t1.Value, t1.date
from table1 t1
join (select id, max(Date) maxdate
from table1
where id in ('AL-1','AL-2')
group by id) t2 on t1.ID = t2.ID and date(t1.Date) = date(t2.maxdate)
where t1.id in ('AL-1','AL-2')发布于 2018-05-09 20:25:10
在limit中使用order by desc
select ID, Value from table1 order by date desc limit 1https://stackoverflow.com/questions/50253338
复制相似问题