常用的有两种方法:
1、单语句法:
1)delete b from sys_test b ,(select *,max(uuid) from sys_test group by `name` having count(`name`) > 1) as d where b.uuid>d.uuid and b.name = d.name
delete b from sys_test b这一句是设置别名,delete语句一般直接接from,当需要设置from表别名是,需要把别名放在from之前,表示删除时是对该别名中内容进行删除。
from中不同的表用,分割,可以有子查询出来的表,后面再接where即可。
2)delete from user where id not in (select Id from (select Max(ID) as Id,姓名,身份证号 from User group by 姓名,身份证号) as t );
该方法效率略低(在MySql下不能执行,或许别的数据库可以执行,MySql下不能执行的原因是from子句后面的表和子查询的from表明不能相同)
DELETE FROM data_item_1432028896553 WHERE item_phone NOT IN ( SELECT item_phone FROM data_item_1432028896553 WHERE uuid = "0065a8b2e59541d0b4af8e26ba5b224e" );
这样是不行的,会报[Err] 1093 - You can't specify target table 'data_item_1432028896553' for update in FROM clause错误,意思就是删除的目标不能和子查询中表名相同。如果想要这样做,也是可以的,孙子查询即可。
DELETE FROM data_item_1432028896553 WHERE item_phone NOT IN ( SELECT * FROM ( SELECT item_phone FROM data_item_1432028896553 WHERE uuid = "0065a8b2e59541d0b4af8e26ba5b224e" ) as t );
再套一层select,三层的时候孙子查询应该就会生成临时表了。
2、临时表法
1)、找出所有重复的数据,并把重复数据复制一条到临时表
select * into #temp1 from user group by `name` having count(`name`) > 1
2)、删除原表中的所有重复的数据
delete from user where name in (select `name` from user group by `name` having count(`name`) > 1);
3)、将临时表中的数据在插入回user表
insert into user select * from #temp1;
4)、删除临时表
drop #temp1;