gpt4 book ai didi

mysql - 在MySQL中加入联接删除

转载 作者:行者123 更新时间:2023-12-02 10:56:09 24 4
gpt4 key购买 nike

这是创建我的表的脚本:

CREATE TABLE clients (
client_i 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

编辑:有关更多信息,您可以看到 this alternative answer

关于mysql - 在MySQL中加入联接删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63599926/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com