DELETE FROM table1
WHERE field1=(SELECT id FROM table2 WHERE type=1)
OR field2=(SELECT id FROM table2 WHERE type=1)
编写此查询的正确方法是什么?它目前不起作用,因为子查询返回不止一行。
DELETE FROM table1
WHERE field1=(SELECT id FROM table2 WHERE type=1)
OR field2=(SELECT id FROM table2 WHERE type=1)
编写此查询的正确方法是什么?它目前不起作用,因为子查询返回不止一行。
使用IN
:
DELETE FROM table1
WHERE field1 IN (SELECT id FROM table2 WHERE type=1)
OR field2 IN(SELECT id FROM table2 WHERE type=1)
你可以这样做IN
:
delete
from table1
where field1 in (
select id
from table2
where type = 1
)
or field2 in (
select id
from table2
where type = 1
)
根据您的桌子的大小,您可能能够摆脱 IN 方法。如果它们在较大的一侧,您可以使用 DELETE...USING 语法。
DELETE FROM foo USING foo, bar WHERE foo.field1=bar.id OR foo.field2=bar.id