开发者

How to filter mysql duplicated id rows and retrieve the one with latest timestamp?

开发者 https://www.devze.com 2023-01-15 09:01 出处:网络
I have a mysql table with entries like this: idnamevaluedate 1results11000000 2010-06-02 01:31:12 2results26开发者_运维技巧000002010-09-03 05:42:54

I have a mysql table with entries like this:

id  name        value   date
1   results1    1000000 2010-06-02 01:31:12
2   results2    6开发者_运维技巧00000  2010-09-03 05:42:54
1   results1    1200000 2010-09-06 02:14:36

How can I SELECT all and filter multiple rows that have the same id, selecting only the one with latest date?

The "date" column datatype is timestamp and it has CURRENT_TIMESTAMP as default.


select m.*
from (
    select id, max(date) as MaxDate
    from MyTable
    group by id
) mm
inner join MyTable m on mm.id = m.id and mm.MaxDate = m.Date
0

精彩评论

暂无评论...
验证码 换一张
取 消