3 回答
TA贡献2080条经验 获得超4个赞
您无法指定要删除的目标表。
解决方法
create table term_hierarchy_backup (tid int(10)); <- check data type
insert into term_hierarchy_backup
SELECT DISTINCT(th1.tid)
FROM term_hierarchy AS th1
INNER JOIN term_hierarchy AS th2 ON (th1.tid = th2.tid AND th2.parent != 1015)
WHERE th1.parent = 1015;
DELETE FROM term_hierarchy AS th
WHERE th.parent = 1015 AND th.tid IN (select tid from term_hierarchy_backup);
TA贡献1995条经验 获得超2个赞
对于其他发现此问题并希望在使用子查询时删除的问题,我将这个示例留给您,以取代MySQL(即使有些人似乎认为无法做到):
DELETE e.*
FROM tableE e
WHERE id IN (SELECT id
FROM tableE
WHERE arg = 1 AND foo = 'bar');
会给你一个错误:
ERROR 1093 (HY000): You can't specify target table 'e' for update in FROM clause
但是这个查询:
DELETE e.*
FROM tableE e
WHERE id IN (SELECT id
FROM (SELECT id
FROM tableE
WHERE arg = 1 AND foo = 'bar') x);
会很好地工作:
Query OK, 1 row affected (3.91 sec)
将子查询包装在另一个子查询(这里称为x)中,MySQL会很乐意完成您的要求。
TA贡献1827条经验 获得超8个赞
别名应包含在DELETE关键字之后:
DELETE th
FROM term_hierarchy AS th
WHERE th.parent = 1015 AND th.tid IN
(
SELECT DISTINCT(th1.tid)
FROM term_hierarchy AS th1
INNER JOIN term_hierarchy AS th2 ON (th1.tid = th2.tid AND th2.parent != 1015)
WHERE th1.parent = 1015
);
添加回答
举报