我有下面提到的表:
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
答案 0 :(得分:1)
如果您想为每个ID
获取最新行,则可以使用相关 select t1.*
from table1 t1
where Date = (select max(t2.Date) from tabel1 t2 where t1.ID = t2.ID);
:
tags => ["transactions"]
tags => ["db_stats_ts"]
答案 1 :(得分:1)
有一个子查询,返回每个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')
答案 2 :(得分:0)
将order by desc
与limit
select ID, Value from table1 order by date desc limit 1
答案 3 :(得分:0)
尝试如下:
SELECT * FROM table t
WHERE t.date =(SELECT MAX(date) FROM table )
答案 4 :(得分:0)
试试这个:
select ID, Value
from table1
where id='AL-1'
group by date desc
limit 1
答案 5 :(得分:0)
monaco.editor.setTheme('vs')