表结构为:
表数据为:
表中数据可以看到name列中有两行数据是一致的,去重的SQL语句为:
1 delete from user where name in (select name from (select name from user group by name having count(name) > 1) as s1) 2 and id not in (select id from (select min(id) as id from user group by name having count(name)>1) as s2);
去重后的数据为:
总结
在进行去重时,数据库无法进行边查询边删除,所以删除的时候必须是第三张临时表作为匹配删除,这样的操作可以省略创建临时表直接删除原表中数据,推荐这种方式进行去重操作。