mysql重複記錄刪除其中一條的辦法

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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章