1
DELETE FROM table1
WHERE field1=(SELECT id FROM table2 WHERE type=1) 
OR field2=(SELECT id FROM table2 WHERE type=1)

编写此查询的正确方法是什么?它目前不起作用,因为子查询返回不止一行。

4

3 回答 3

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)
于 2012-09-11T19:57:53.300 回答
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
        )
于 2012-09-11T19:57:41.813 回答
1

根据您的桌子的大小,您可能能够摆脱 IN 方法。如果它们在较大的一侧,您可以使用 DELETE...USING 语法。

DELETE FROM foo USING foo, bar WHERE foo.field1=bar.id OR foo.field2=bar.id
于 2012-09-11T20:08:07.850 回答