alter ignore table 表名 add UNIQUE index(字段名);
然后再将索引drop掉,这个方法外。
更加中规中矩的方法也是有的。。
比如我在网上搜到的:
deletefrompeople
wherepeopleIdin(selectpeopleIdfrompeoplegroupbypeopleIdhavingcount(peopleId)>1)
and idnotin(selectmin(id)frompeoplegroupbypeopleIdhavingcount(peopleId)>1)
这个看起来好像很有道理,但是执行的时候就会报错。。You can't specify target table 'people' for update in FROM clause。
原因就是上面的那一条SQL,删除和查询操作都是针对同一张表的。。MYSQL是不允许这样做滴。
这样就可以了
deletefrompeople
wherepeopleIdin(selectpeopleIdfrom(select id,peopleId from people) as a groupbypeopleIdhavingcount(peopleId)>1)
and idnotin(selectmin(id)from (select id,peopleId from people) as b groupbypeopleIdhavingcount(peopleId)>1)
简单一点的可以这样写
delete from id where Id not in (select min(Id) from(select
id,peopleId from people)as a GROUP by peopleId );
本文介绍了一种在MySQL中删除重复记录的有效方法。通过使用子查询和派生表的方式,避免了直接在同一表上进行更新和选择操作所导致的问题。文中提供了具体的SQL语句实例。
472

被折叠的 条评论
为什么被折叠?



