gpt4 book ai didi

mysql - Fun with MySQL - 如何使用 IN 编写删除语句

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

当我尝试

DELETE FROM `TreePaths` WHERE `descendant` IN (SELECT `descendant` FROM `TreePaths` WHERE `ancestor`= 0x04);

我明白了

#1093 - You can't specify target table 'TreePaths' for update in FROM clause

我怎样才能让删除生效?

更新:表结构:

CREATE TABLE TreePaths (
ancestor VARBINARY(16) NOT NULL,
descendant VARBINARY(16) NOT NULL,
PRIMARY KEY (`ancestor`, `descendant`),
FOREIGN KEY (`ancestor`) REFERENCES Library(iD),
FOREIGN KEY (`descendant`) REFERENCES Library(iD)
);

表格数据:

ancestor    descendant
01 01
02 02
02 03
03 03
02 04
04 04
02 05
04 05
05 05
02 06
04 06
06 06
07 07
08 08
09 09
10 10

最佳答案

在 MySQL 中更容易进行多表删除:

DELETE paths_to_subtree FROM `TreePaths` AS paths_to_subtree
JOIN `TreePaths` AS subtree USING (`descendant`)
WHERE subtree.`ancestor`= 0x04;

关于mysql - Fun with MySQL - 如何使用 IN 编写删除语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8191326/

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