mysql重复记录删除其中一条的办法

来源:互联网 发布:软件开发专业大学排名 编辑:程序博客网 时间:2024/04/30 11:54

场景:出现了两条一模一样的记录,除了例如createime和id,其他都相同,现需要删除例如重复记录中id小的,保留一条且id较大的。


SQL:

select * from 表名 where 值A in (select 值A from 表名 where 值B = 'B值' group by 值A having count(值A) > 1) and id not in (select min(id) from 表名 where 值B = 'B值' group by 值A having count(值A) > 1) and 值B = 'B值';

查出来然后删掉即可。

若采用如下方式会报错,解决方式待更新:

delete from 表名 where 值A in (select 值A from 表名 where 值B = 'B值' group by 值A having count(值A) > 1) and id not in (select min(id) from 表名 where 值B = 'B值' group by 值A having count(值A) > 1) and 值B = 'B值';

报错:

[Err] 1093 - You can't specify target table '表名' for update in FROM clause


0 0