MySQLi:使用1个查询从三个不同的表中删除?

时间:2013-10-29 11:37:03

标签: mysql sql mysqli

是否可以将这三个查询合并为一个?

DELETE FROM `users exercises` WHERE `date` = '$date' AND `userid` = '$user->id'
DELETE FROM `users foods` WHERE `date` = '$date' AND `userid` = '$user->id'
DELETE FROM `users check-ins` WHERE `date` = '$date' AND `userid` = '$user->id'

1 个答案:

答案 0 :(得分:2)

如果可以加入,可以从多个表中删除。在你的情况下也是如此。尝试

DELETE e, f, c
FROM `users exercises` e
inner join `users foods` f on f.userid = e.userid and f.`date` = e.`date`
inner join `users check-ins` c on c.userid = e.userid and c.`date` = e.`date`
WHERE e.`date` = '$date' 
AND e.userid = '$user->id'