SQL:使用联接从多个表中删除

时间:2013-10-29 12:48:08

标签: php mysql sql join mysqli

如果三个表中的每一个都产生结果,则以下内容将起作用,但是如果这些表中只有一个没有任何内容,那么它会破坏查询并且不会删除任何内容。有没有办法防止这个查询,所以如果三个表中只有一个有结果,它们将被删除?

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'

1 个答案:

答案 0 :(得分:1)

你有'用户'表吗?如果是这样,那么你可以这样写:

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