问题
CREATE TABLE `clients` (
   `client_id` int(11),
   PRIMARY KEY (`client_id`)
);
CREATE TABLE `projects` (
   `project_id` int(11) unsigned,
   `client_id` int(11) unsigned,
   PRIMARY KEY (`project_id`)
);
CREATE TABLE `posts` (
   `post_id` int(11) unsigned,
   `project_id` int(11) unsigned,
   PRIMARY KEY (`post_id`)
);

在我的PHP代码中,删除客户端时,我要删除所有项目的帖子:

DELETE 
FROM posts

INNER JOIN projects 
  ON projects.project_id = posts.project_id

WHERE projects.client_id = :client_id;

posts表没有外键 client_id ,只有 project_id .我想删除已发送到已通过 client_id 的项目中的帖子.

目前无法正常运作(没有讯息会被删除).



解决方法

您只需要指定要从 posts 表中删除条目:

DELETE posts
FROM posts
INNER JOIN projects ON projects.project_id = posts.project_id
WHERE projects.client_id = :client_id

EDIT:有关详情,请参阅此替代答案




相关问题推荐