SQL DELETE 并 JOIN 另一个表作为 WHERE 条件
我必须从 guide_category
中删除与 guide
表没有关系的行(死关系)。
这就是我想做的,但它当然行不通。
DELETE FROM guide_category AS pgc
WHERE pgc.id_guide_category IN (SELECT id_guide_category
FROM guide_category AS gc
LEFT JOIN guide AS g ON g.id_guide = gc.id_guide
WHERE g.title IS NULL)
错误:
您无法在 FROM 子句中指定要更新的目标表“guide_category”
I have to delete rows from guide_category
that have no relation with guide
table (dead relations).
Here is what I want to do, but it of course does not work.
DELETE FROM guide_category AS pgc
WHERE pgc.id_guide_category IN (SELECT id_guide_category
FROM guide_category AS gc
LEFT JOIN guide AS g ON g.id_guide = gc.id_guide
WHERE g.title IS NULL)
Error:
You can't specify target table 'guide_category' for update in FROM clause
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于锁定实现问题,
MySQL
不允许使用DELETE
或UPDATE
引用受影响的表。您需要在此处创建
JOIN
:或者仅使用
NOT IN
:Due to the locking implementation issues,
MySQL
does not allow referencing the affected table withDELETE
orUPDATE
.You need to make a
JOIN
here instead:or just use a
NOT IN
:我认为,根据您的描述,以下内容就足够了:
我认为所涉及的表没有引用完整性约束,是吗?
I think, from your description, the following would suffice:
I assume, that there are no referential integrity constraints on the tables involved, are there?
尝试这个示例 SQL 脚本以便于理解,
您的情况是:
Try this sample SQL scripts for easy understanding,
Your case is:
怎么样:
How about: