如何使用1个查询执行此操作?

时间:2013-01-30 14:49:00

标签: php

delete from table1 where user_id = ...
delete from table2 where user_id = ...
delete from table3 where user_id = ...
delete from table4 where user_id = ...

我尝试从这1个查询中获取,但不是结果。

1 个答案:

答案 0 :(得分:0)

我认为这会起作用

DELETE table1.*, table2.*,table3.* FROM table1, table2, table3 
WHERE table1.user_id=? and table2.user_id=? and table3.user_id=?; 

还有另一种解决方案可行:

DELETE FROM table1, table2, table3 
USING table1 
    INNER JOIN table2 USING(user_id) 
    INNER JOIN table3 USING(user_id) 
WHERE table1.user_id= ?

无论如何,您可以使用ON DELETE CASCADE选项在表上定义foreign key constraints,然后从父表中删除记录将删除子表中的所有记录。