3 回答

TA貢獻(xiàn)2080條經(jīng)驗(yàn) 獲得超4個(gè)贊
您無法指定要?jiǎng)h除的目標(biāo)表。
解決方法
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貢獻(xiàn)1995條經(jīng)驗(yàn) 獲得超2個(gè)贊
對(duì)于其他發(fā)現(xiàn)此問題并希望在使用子查詢時(shí)刪除的問題,我將這個(gè)示例留給您,以取代MySQL(即使有些人似乎認(rèn)為無法做到):
DELETE e.*
FROM tableE e
WHERE id IN (SELECT id
FROM tableE
WHERE arg = 1 AND foo = 'bar');
會(huì)給你一個(gè)錯(cuò)誤:
ERROR 1093 (HY000): You can't specify target table 'e' for update in FROM clause
但是這個(gè)查詢:
DELETE e.*
FROM tableE e
WHERE id IN (SELECT id
FROM (SELECT id
FROM tableE
WHERE arg = 1 AND foo = 'bar') x);
會(huì)很好地工作:
Query OK, 1 row affected (3.91 sec)
將子查詢包裝在另一個(gè)子查詢(這里稱為x)中,MySQL會(huì)很樂意完成您的要求。

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個(gè)贊
別名應(yīng)包含在DELETE關(guān)鍵字之后:
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
);
添加回答
舉報(bào)