我正在使用以下查询来删除不在另一个表中的数据。
delete from table_1 where tbl_id in
(select tbl_id from table_1 left join table_2
on table_1.tbl_id=table_2.another_tbl_id where table_2.another_tbl_id is null)
以上查询中是否有任何问题?
它显示您无法在FROM子句中为更新指定目标表'table_1'。
如何解决这个问题?
答案 0 :(得分:0)
试试这个,
DELETE t1 FROM tablename1 t1 LEFT JOIN tablename2 tn2
ON t1.tbl_id=tn2.an`enter code here`other_tbl_id WHERE tn2.another_tbl_id IS NULL
答案 1 :(得分:0)
您必须使用JOIN
而不是IN
DELETE table_1.* FROM table_1
LEFT JOIN table_2 ON table_1.tbl_id=table_2.another_tbl_id
WHERE table_2.another_tbl_id IS NULL
答案 2 :(得分:0)
解决了我的问题。
我修改了上面的查询如下:
delete from table_1 where tbl_id not in
(select another_tbl_id from table_2)
感谢您的所有回复......
答案 3 :(得分:0)
最简单的方法就在这里......
DELETE t1 FROM table_1 t1 WHERE t1.tbl_id NOT IN
(SELECT another_tbl_id FROM table_2)