从多个表中删除不起作用

时间:2015-10-06 07:02:17

标签: php mysql sql

场景:有4个表[A,B,C,D,E]。我想在一个查询中删除所有这些。我正在应用以下查询。

$con=mysqli_connect("localhost","root","","db")
$del="DELETE a,b,c,d,e FROM `A` as a INNER JOIN `B` as b INNER JOIN `C` as c  
INNER JOIN
 `D` as d INNER JOIN `E` as e ON a.user_id=b.user_id AND a.user_id=c.user_id     
AND a.user_id=d.user_id AND a.user_id=e.user_id 
WHERE login.user_id= '25'";
$que=mysqli_query($con,$del)
if($del){
echo "Its done";
}
else{
echo mysqli_error($con);}

当每个表中的数据与该ID相对时,它正在删除。但是当某些表中没有数据时,请在D和E中说,查询不起作用,然后它不会删除任何内容。我认为它是因为我把AND运算符放在'ON'之间。我对sql了解不多。所以,有人请帮助我。还有一件事是它没有给出任何错误它的打印完成。 希望对你有意义。谢谢。

2 个答案:

答案 0 :(得分:0)

每个联接都有自己的ON子句

DELETE a,b,c,d,e 
FROM `A` as a 
INNER JOIN `B` as b ON a.user_id=b.user_id
LEFT JOIN `C` as c ON a.user_id=c.user_id  
INNER JOIN `D` as d ON a.user_id=d.user_id
LEFT JOIN `E` as e ON a.user_id=e.user_id    
WHERE a.user_id= '25'

答案 1 :(得分:0)

试试这个

$del="DELETE a,b,c,d,e 
    FROM `A` as a 
    LEFT OUTER JOIN `B` as b ON a.user_id=b.user_id
    LEFT OUTER JOIN `C` as c ON a.user_id=c.user_id
    LEFT OUTER JOIN `D` as d ON a.user_id=d.user_id
    LEFT OUTER JOIN `E` as e ON a.user_id=e.user_id
WHERE login.user_id= '25'";