Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何获取不同用户名行的最大值_Mysql - Fatal编程技术网

Mysql 如何获取不同用户名行的最大值

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

下面是我的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,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描述从表组中选择用户名、日志文件;