- 在mySQL中,如何获取插入的最新数据?

我有这种表:


ID NUMBER DATE_INPUT


 A 1 2019-05-26


 A 2 2019-05-27


 A 0 2019-05-28


 B 0 2019-05-27


 B 1 2019-05-28



我使用GROUP BY来获取分组数据,结果是:


 ID NUMBER DATE_INPUT 


 A 1 2019-05-26


 B 0 2019-05-27



所需的输出为:


 ID NUMBER DATE_INPUT 


 A 0 2019-05-28


 B 1 2019-05-28



时间:

你可以使用相关子查询


select * from tablename a


where date_input = (select max(date_input) from tablename b


where a.id=b.id)



由于你有日期输入字段因此,可以使用GROUP和ORDER获取所需内容:

SELECT ID, NUMBER, DATE_INPUT FROM MyTable GROUP BY ID ORDER BY DATE_INPUT DESC

尝试此代码。

select l.* from table l inner join ( select date_input as latest from table group by id order by date_input desc ) r on l.date_input= r.latest and l.id= r.id order by date_input desc

使用ORDER BY desc


select * from tablename group by ID order by date_input desc



...