您好,欢迎访问一九零五行业门户网

MySQL 删除数据库中重复数据方法小结_MySQL

刚开始,根据我的想法,这个很简单嘛,上sql语句
delete from zqzrdp where tel in (select min(dpxx_id) from zqzrdp group by tel having count(tel)>1);
执行,报错!!~!~
异常意为:你不能指定目标表的更新在from子句。傻了,mysql 这样写,不行,让人郁闷。
难倒只能分步操作,蛋疼
以下是网友写的,同样是坑爹的代码,我机器上运行不了。
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 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)
看来想偷懒使用一句命令完成这个事好像不太显示,还是老老实实的分步处理吧,思路先建立复制一个临时表,然后对比临时表内的数据,删除主表里的数据
alter table tablename add autoid int auto_increment not null; create table tmp select min(autoid) as autoid from tablename group by name,address; create table tmp2 select tablename.* from tablename,tmp where tablename.autoid = tmp.autoid; drop table tablename; rename table tmp2 to tablename;
其它类似信息

推荐信息