3 回答

TA貢獻2080條經(jīng)驗 獲得超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條經(jīng)驗 獲得超2個贊
對于其他發(fā)現(xiàn)此問題并希望在使用子查詢時刪除的問題,我將這個示例留給您,以取代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條經(jīng)驗 獲得超8個贊
別名應(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
);
添加回答
舉報