Mysql 如何获取不同用户名行的最大值
下面是我的sql表Mysql 如何获取不同用户名行的最大值,mysql,Mysql,下面是我的sql表 id,username,logfile 1,admin,20200521-072805.txt 2,admin,20200521-072845.txt 3,admin1,20200521-072857.txt 4,admin1,20200521-074933.txt 5,admin2,20200521-075857.txt 6,admin2,20200521-076852.txt 预料之外 2,admin,20200521-072845.txt 4,admin1,20200
id,username,logfile
1,admin,20200521-072805.txt
2,admin,20200521-072845.txt
3,admin1,20200521-072857.txt
4,admin1,20200521-074933.txt
5,admin2,20200521-075857.txt
6,admin2,20200521-076852.txt
预料之外
2,admin,20200521-072845.txt
4,admin1,20200521-074933.txt
6,admin2,20200521-076852.txt
我想从mysql表中获取最新数据
我的查询提供了所有数据中的最新数据
SELECT distinct(username),log_file FROM table WHERE id=(SELECT max(id) FROM table);
使用相关子查询
select username,logfile
from table t
where id = (select max(id) from table t1 where t1.username = t.username)
我想你只需按用户名分组,按id排序就可以了。按用户名顺序按id描述从表组中选择用户名、日志文件;