我最常用的方法是
代码如下 | 复制代码 |
//删除id重复的数据,适合id是手工主键 delete person as a from person as a, ( select *,min(id) from person group by id having count(1) > 1 ) as b where a.id = b.id |
//查找重复的,并且除掉最小的那个
代码如下 | 复制代码 |
delete tb_person as a from tb_person as a, ( select *,min(id) from tb_person group by name having count(1) > 1 ) as b where a.name = b.name and a.id > b.id; |
好了下面再总结一些
1. 查询需要删除的记录,会保留一条记录。
代码如下 | 复制代码 |
select a.id,a.subject,a.RECEIVER from test1 a left join (select c.subject,c.RECEIVER ,max(c.id) as bid from test1 c where status=0 GROUP BY RECEIVER,SUBJECT having count(1) >1) b on a.id< b.bid where a.subject=b.subject and a.RECEIVER = b.RECEIVER and a.id < b.bid |
2. 删除重复记录,只保留一条记录。注意,subject,RECEIVER 要索引,否则会很慢的。
代码如下 | 复制代码 |
delete a from test1 a, (select c.subject,c.RECEIVER ,max(c.id) as bid from test1 c where status=0 GROUP BY RECEIVER,SUBJECT having count(1) >1) b where a.subject=b.subject and a.RECEIVER = b.RECEIVER and a.id < b.bid; |
3. 查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断
代码如下 | 复制代码 |
select * from people where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1) |
4. 删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录
代码如下 | 复制代码 |
delete from people where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1) and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1) |
5.删除表中多余的重复记录(多个字段),只留有rowid最小的记录
代码如下 | 复制代码 |
delete from vitae a where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1) and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1) |
说了这么多了我们来个例子吧
例子如下:
代码如下 | 复制代码 |
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); mysql> select * from t_user; mysql> delete t_user from t_user , (select id from t_user group by username having count(*)>1 ) as t2 where t_user.id=t2.id; mysql> select * from t_user; |