当我试着
DELETE FROM `TreePaths` WHERE `descendant` IN (SELECT `descendant` FROM `TreePaths` WHERE `ancestor`= 0x04);
我得到
#1093-不能为UPDATE IN FROM子句指定目标表“treepath”
如何才能让删除生效?
更新:
表格结构:
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;