4

エラー 1093 は、サブクエリが削除元のテーブルをクエリする場合、サブクエリを使用して UPDATE または DELETE できないことを示しています。

だからあなたはできません

delete from table1 where id in (select something from table1 where condition) ;

わかりました、その制限を回避する最善の方法は何ですか?

編集:

興味のある人への質問は次のとおりです。

mysql> desc adjacencies ;
+--------+---------+------+-----+---------+------ -+
| | フィールド | フィールド タイプ | ヌル | キー | キー | デフォルト | エクストラ |
+--------+---------+------+-----+---------+------ -+
| | 親| int(11) | いいえ | PRI | ヌル | | |
| | 子 | int(11) | いいえ | PRI | ヌル | | |
| | パスレン | int(11) | いいえ | | | ヌル | | |
+--------+---------+------+-----+---------+------ -+



-- クエリは
-- すべての子供たちにそう言いなさい
-- 私の年老いた両親のことを考えるのをやめる
-- まだ両親です

隣接から削除
親がいる場所
(
-- すべての私の両親、祖父母
  親を選択
  隣接から
  ここで child=@me
  and parent!=@me
)

-- 私の関係だけに関係する
-- 祖父母 私の子供が心配している場所
と子
(
  -- すべての子を取得します
  子を選択
  隣接から
  ここで、parent=@me
)

;

だから私がこれまでに試したことは、という一時テーブルを作成することですadjsToDelete

create temporary table adjsToRemove( parent int, child int ) ;
insert into adjsToRemove...

これで、削除するリレーションのコレクションができました。ここで、親/子のペアはそれぞれ、削除する行を一意に識別します。しかし、隣接テーブルから各ペアを削除するにはどうすればよいでしょうか?

の各エントリに一意のauto_incremented キーを追加する必要があるようですが、そうadjacenciesですか?

4

5 に答える 5

8

http://bugs.mysql.com/bug.php?id=6980にある回避策は、アイテムを返すサブクエリのエイリアスを作成することです。それで

delete from table1 where id in 
  (select something from table1 where condition)

に変更されます

delete from table1 where id in
  (select p.id from (select something from table1 where condition) as p)
于 2013-03-21T14:27:38.730 に答える
2

できるよ

delete t1,t2 
FROM  table1 t1  
INNER JOIN 
table1 t2 ON (t2.something = t1.id);

質問のクエリの場合、これは同等である必要があります。

delete A
from adjacencies A
join adjacencies B ON A.parent = B.parent AND B.child=@me AND B.parent != @me
join adjacencies C ON A.child = C.child AND C.parent=@me
于 2010-12-19T20:18:47.887 に答える
1

簡略化:

-- Collect ids
CREATE TEMPORARY TABLE cleanup_lookup AS 
SELECT id FROM table1 WHERE condition;

-- Delete the selected records
DELETE t FROM table1 t INNER JOIN cleanup_lookup l ON t.id = l.id;

-- Temporary tables get dropped when the connection is closed.
于 2013-03-12T12:02:59.853 に答える
1

Currently, you cannot delete from a table and select from the same table in a subquery -詳細

削除対象のテーブルを指定することはできませ

私の回避策の1つ:条件としてサブクエリを使用したMySQL DELETE FROM

于 2010-12-20T02:43:23.097 に答える