2012-09-11 89 views

回答

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) 
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 
     ) 
1

根據您的表的大小,你也許可以逃脫的IN-方法。如果它們在較大的一面,則可以使用DELETE ... USING語法。

DELETE FROM foo USING foo, bar WHERE foo.field1=bar.id OR foo.field2=bar.id 
相關問題