gpt4 book ai didi

MySQL - 当嵌套选择使用该表时如何从表中删除?

转载 作者:可可西里 更新时间:2023-11-01 08:07:55 26 4
gpt4 key购买 nike

我正在尝试做这样的事情:

DELETE FROM table_1
WHERE table_1.id IN (SELECT table_1.id
FROM table_1,
table_2,
table_3
WHERE table_1.id = table_2.table_1_id
AND table_2.id = table_3.table_2_id
AND table_3.id = 5)

似乎 MySQL 不允许我在嵌套的 SELECT 中包含 table_1。有什么建议吗?

最佳答案

您可能遇到此错误:

ERROR 1093 (HY000): You can't specify target table 'table_1' for update in FROM clause.

你应该能够使用 MySQL 的多表来做到这一点 DELETE语法改为:

DELETE table_1.*
FROM table_1,
table_2,
table_3
WHERE table_1.id = table_2.table_1_id
AND table_2.id = table_3.table_2_id
AND table_3.id = 5

上面的查询应该有效,但作为一般规则,我建议改用 ANSI JOIN 语法:

DELETE table_1.*
FROM table_1
INNER JOIN table_2 on table_2.table_1_id = table_1.id
INNER JOIN table_3 on table_3.table_2_id = table_2.id
WHERE table_3.id = 5

关于MySQL - 当嵌套选择使用该表时如何从表中删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6038470/

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