Delete with Join in MySQL(在 MySQL 中使用 Join 删除)
问题描述
这是创建我的表的脚本:
Here is the script to create my tables:
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 代码中,删除客户端时,我想删除所有项目帖子:
In my PHP code, when deleting a client, I want to delete all projects posts:
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 的帖子.
The posts table does not have a foreign key client_id, only project_id. I want to delete the posts in projects that have the passed client_id.
这现在不起作用,因为没有帖子被删除.
This is not working right now because no posts are deleted.
推荐答案
您只需要指定要从 posts 表中删除条目:
You just need to specify that you want to delete the entries from the posts table:
DELETE posts
FROM posts
INNER JOIN projects ON projects.project_id = posts.project_id
WHERE projects.client_id = :client_id
有关更多信息,您可以查看此替代答案
For more information you can see this alternative answer
这篇关于在 MySQL 中使用 Join 删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 MySQL 中使用 Join 删除
基础教程推荐
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- 无法解决整理冲突 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
