MySQL查询的语句求助,查询不重复一览

MySQL查询的语句求助,查询不重复一览

问题描述:

有如下的表:
CREATE TABLE loginlog (
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
username VARCHAR(100) DEFAULT NULL,
operate VARCHAR(512) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=MYISAM AUTO_INCREMENT=1 DEFAULT CHARSET=gbk

表内相同username的数据很多,id是自动增量列;
希望用一条SQL语句,查出最新id的username+operate一览,要求结果内username不重复,请帮忙写SQL语句,谢谢!

查出每个用户的最新一条记录,思路:先倒序,再group分组

select a.* from (select * from loginlog order by id desc) a group by a.username

根据username分组查

select id,username,operate from loginlog group by username order by id desc