简体   繁体   中英

Delete from multiple tables is not working

Scenario: There are 4 tables [A,B,C,D,E]. I wanna delete from all of them in one query. And i'm applying the following query.

$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);}

It's deleting fine when there is data in every table against that id. But when there is no data in some table lets say in D and E, the query doesn't work and it doesn't delete anything then. I think its because i have put AND operator between the 'ON'. I don't know much about sql. So, someone please help me. And one more thing its not giving any errors its printing its done. Hope it makes sense to you.Thanks in advance.

Every join has its own ON clause

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'

Try this

$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'";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM