MYSQL输出仅根据最新时间结果

时间:2015-08-17 05:11:19

标签: mysql

这是数据库:

to     from     time
A      B        2015-01-02 12:00:30
B      A        2015-01-02 12:30:40
A      B        2015-01-02 12:20:30
A      C        2015-01-02 13:00:30
C      A        2015-01-02 13:20:30

如何输出以下结果:

to     from     time
B      A        2015-01-02 12:30:40
C      A        2015-01-02 13:20:30

所以我在为每对记者保留最新时间的行之后。

THX!

2 个答案:

答案 0 :(得分:0)

SELECT to, from, time
FROM table
WHERE time IN (SELECT max(time)
               FROM table
               GROUP BY HOUR(time))

答案 1 :(得分:-1)

select top(2) * from table order by time desc

如果时间没有正确分类,你可以

select top(2) * from table order by cast( SUBSTRING_INDEX(SUBSTRING_INDEX(time, ':', 1), ':', -1) as int)*60+Cast( SUBSTRING_INDEX(SUBSTRING_INDEX(time, ':', 2), ':', -1) as int) desc