mysql->sql一句sql刪除重復數據 面試常考的一道題:一句sql刪除表裡的重復數據。 偶爾和同事聊到這個問題就順便寫了下代碼,供大家參考~ //數據准備 Mysql代碼 drop table t_user; create table t_user( id int(5) not null auto_increment, username varchar(10), age int(3), primary key(id) ); insert into t_user(username,age) values('aaa',20); insert into t_user(username,age) values('aaa',20); insert into t_user(username,age) values('aaa',20); insert into t_user(username,age) values('bbb',20); insert into t_user(username,age) values('bbb',20); insert into t_user(username,age) values('ccc',20); insert into t_user(username,age) values('ccc',20); insert into t_user(username,age) values('ddd',20); insert into t_user(username,age) values('ddd',20); 刪除語句: Mysql代碼 DELETE t FROM t_user t, ( SELECT min(id)AS ttid, username FROM t_user t2 GROUP BY t2.username )AS tt WHERE t.id > tt.ttid and t.username = tt.username;