DELETE FROM table1
WHERE field1=(SELECT id FROM table2 WHERE type=1)
OR field2=(SELECT id FROM table2 WHERE type=1)
编写此查询的正确方法是什么?它目前不起作用,因为子查询返回多行。
答案 0 :(得分:5)
使用IN
:
DELETE FROM table1
WHERE field1 IN (SELECT id FROM table2 WHERE type=1)
OR field2 IN(SELECT id FROM table2 WHERE type=1)
答案 1 :(得分:3)
您可以使用IN
:
delete
from table1
where field1 in (
select id
from table2
where type = 1
)
or field2 in (
select id
from table2
where type = 1
)
答案 2 :(得分:1)
根据表格的大小,您可以使用IN方法。如果他们在更大的一面,你可以使用DELETE ... USING语法。
DELETE FROM foo USING foo, bar WHERE foo.field1=bar.id OR foo.field2=bar.id