場景:出現了兩條一模一樣的記錄,除了例如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