mysql中,打算使用exists查找相關記錄,然後删除,結果搞不定。
我們有個表folder,裡面有個字段code存在重複值,本來編碼應該是唯一的。是以想将重複記錄删除。
這樣
delete from folder fd1 where exists(
select 1 from folder fd2 where id<>fd1.id and code=fd1.code);
不行!
這樣
delete from folder where id in(
select id from folder fd1 where exists(
select 1 from folder fd2 where id<fd1.id and code=fd1.code));
還是不行!
查找網上的例子:
DELETE tb
FROM tb LEFT JOIN
tb tb2
ON tb2.`merchantId` = 'A32WNPGI8GE4WW' AND
tb2.`marketplaceId` IN ('A1AM78C64UM0Y8', 'A2EUQ1WTGCTBG2', 'ATVPDKIKX0DER')
WHERE tb2.merchantID IS NULL;
create temporary table tmp_folder_id as select id from folder fd1 where exists(select 1 from folder fd2 where id<fd1.id and code=fd1.code);
delete from folder where id in(select id from tmp_folder_id);
drop table tmp_folder_id;