只需合并2删除查询即可

时间:2014-01-18 19:16:31

标签: mysql sql

我有2个删除SQL查询我想让它成为一个。

$sql1= DELETE FROM user WHERE userid={$id}

另一个是

$sql = Delete FROM user_meta where userid = {$id}

有什么建议吗?我想简单化。

2 个答案:

答案 0 :(得分:0)

您无法从DELETE语句中的2个表中删除,但是您在1个中发出2个语句:

$sql = "DELETE FROM user WHERE userid={$id};
DELETE FROM user_meta WHERE meta_key = {$id};"

答案 1 :(得分:0)

“同时”从不同表中删除内容的正常方法是将它们包装在事务中。类似的东西:

begin transaction thedeletes
    DELETE FROM user WHERE userid={$id};
    DELETE FROM user_meta WHERE userid = {$id}
commit transaction thedeletes;

在MySQL中,您实际上可以将它们放在同一个查询中:

delete u, um
    from user u join
         user_meta um
         on u.userid={$id} and um.userid = {$id};