SELECT track_id, rider_id, report_time FROM order_track GROUP BY rider_id Order By report_time Desc
取到的值
10420 10006 2017-03-30 20:35:58
10419 10001 2017-03-30 20:34:17
10109 10004 2017-03-29 18:28:10
10108 10005 2017-03-29 18:27:54
10001 10003 2017-03-29 12:47:37
但rider_id 10003 最新值应该是
10418 2017-03-30 18:11:04 10003
更换了网上的写法
SELECT * FROM (SELECT track_id, rider_id, report_time FROM order_track Order By report_time Desc) order_track GROUP BY order_track.rider_id
取到的值
10419 10001 2017-03-30 20:34:17
10001 10003 2017-03-29 12:47:37
10109 10004 2017-03-29 18:28:10
10108 10005 2017-03-29 18:27:54
10420 10006 2017-03-30 20:35:58
order by操作无效
选了个备用写法
SELECT track_id, rider_id, report_time FROM order_track WHERE track_id IN (SELECT max(track_id) FROM order_track GROUP BY rider_id)
取值为
10108 10005 2017-03-29 18:27:54
10109 10004 2017-03-29 18:28:10
10418 10003 2017-03-30 18:11:04
10419 10001 2017-03-30 20:34:17
10420 10006 2017-03-30 20:35:58
补充老司机的解释
group by 后,查询字段不是聚类函数就是group 的字段, sql标准里,这种写法是错的。但是mysql可以运行,属于一个功能,会随机取一条数据。此写法在mssql里报错。
来个老司机的写法
SELECT max(track_id) track_id, rider_id,max(report_time)report_time FROM order_track GROUP BY rider_id
取值为
10419 10001 2017-03-30 20:34:17
10418 10003 2017-03-30 18:11:04
10109 10004 2017-03-29 18:28:10
10108 10005 2017-03-29 18:27:54
10420 10006 2017-03-30 20:35:58