如何获取mysql中最大日期的记录

时间:2016-08-09 06:35:42

标签: mysql

device_id                   out_date                      name
1                       '2015-09-24 11:00:23'             a
4                       '2015-09-23 11:00:23'             x
5                       '2015-09-22 11:00:23'             b
2                       '2015-09-29 11:00:23'             v
3                       '2015-09-23 11:00:23'             v
1                       '2015-09-212 11:00:23'            t
4                       '2015-09-15 11:00:23'             m
5                      '2015-09-18 11:00:23'              b
2                       '2015-09-20 11:00:23'             a             
3                       '2015-09-2 11:00:23'              s
1                       '2015-09-5 11:00:23'              s
4                     '2015-09-7 11:00:23'                s
5                      '2015-09-8 11:00:23'               s
2                     '2015-09-9 11:00:23'                e
3                    '2015-09-4 10:00:23'                 e
1                    '2015-09-7 7:00:23'                  s
4                     '2015-09-21 10:00:23'               s 
5                    '2015-09-23 12:00:23'                w
2                    '2015-09-1 10:00:23'                 s
3                     '2015-09-23 7:00:23'                s

这是我的表我想要记录,其设备ID的date_max就像我们有5个设备ID所以它会喜欢1 max(out_date)和name,2 max(out_date)和name,3 max(out_date)和name .. ..请建议我如何实现它,因为我正在通过

获取组错误

我想要这样的输出

1       max(out_date)     name
2       max(out_date)     name
3        max(out_date)    name
4        max(out_date)    name  
5       max(out_date)      name

2 个答案:

答案 0 :(得分:1)

检查一下:

select a.id, maxoutdate,b.name from
(select id,max(outdate) as maxoutdate from
(select id,max(outdate),outdate from tbl_device_outdate GROUP BY outdate ORDER BY id) as a GROUP BY id) as a

left JOIN

(select * from tbl_device_outdate) as b
on a.id = b.id and a.maxoutdate = b.outdate

答案 1 :(得分:-1)

SELECT
  device_id,
  MAX(out_date)
FROM
  your_table
GROUP BY
  device_id

如果错误仍然存​​在,请发布错误和您的查询。