mysql delete not inmysql
想要執行:sql
DELETE FROM tb_tableA WHERE id IN ( SELECT a.id FROM tb_tableA a WHERE a.id NOT IN ( SELECT a_id FROM tb_tableB ) );
是沒法正確執行的。spa
解決方案:建立臨時表,做爲中間表;用完再刪去。code
CREATE TABLE tmp AS SELECT t.id FROM ( SELECT a.id FROM tb_tableA a WHERE a.id NOT IN ( SELECT a_id FROM tb_tableB ) ) t; DELETE FROM tb_tableA WHERE id IN (SELECT id FROM tmp); DROP TABLE tmp;
【說明】:tb_tableB 中 字段 a_id 是 tb_tableA 的主鍵 id .
成功!blog