按日期查询最新记录,按序列列查询

时间:2016-05-01 03:38:03

标签: mysql

我有这样的数据

--------------------------------
Date                Serial   State
2016-04-30 20:34:47     4    0
2016-04-30 20:34:48     5    0
2016-04-30 20:35:10     4    0
2016-04-30 20:35:08     5    1

我正在尝试使用它的关联状态查询每个序列的最新时间戳,所以它就像这样

Date                Serial   State
2016-04-30 20:35:10     4    0
2016-04-30 20:35:08     5    1

这似乎抓住了我最新的日期并对连续出版物进行了分组,但是州列没有因某种原因而改变并保持在0

Select MAX(date) as date, serial AS serial, state AS state
FROM testGraph
GROUP BY serial

1 个答案:

答案 0 :(得分:1)

您可以在where子句中执行此操作:

select tg.*
from testgraph tg
where tg.date = (select max(tg2.date) from testgraph tg2 where tg2.serial = tg.serial);