0

MySqlに次のステートメントがあります:

delete from match_custom_field_team_value
where match_id=10648 
      and match_custom_field_id=14917 
      and event_id in (2, 6, 8, 4)
      and team_id in (select mcfv2.team_id from match_custom_field_team_value mcfv2 
                        where mcfv2.match_id=10648 and mcfv2.match_custom_field_id=14917 and mcfv2.event_id=9);

これを実行しようとすると、次のようになります。

Error Code: 1093. You can't specify target table 'match_custom_field_team_value' for update in FROM clause

エラーが発生する理由と、エラーを回避するために書き直す最善の方法はありますか? (一時テーブルでできることはわかっていますが、余分な手順は実行したくありません。)

ありがとう、

ジャレド

4

2 に答える 2

1

テーブルから選択して、1 つのクエリで更新/削除することはできません。禁止されていますが、汚い方法で回避できます。

delete from match_custom_field_team_value
where match_id=10648 
  and match_custom_field_id=14917 
  and event_id in (2, 6, 8, 4)
  and team_id in (
     select * from (
         select mcfv2.team_id from match_custom_field_team_value mcfv2 
            where mcfv2.match_id=10648 and mcfv2.match_custom_field_id=14917 and mcfv2.event_id=9)
     ) as sub
  )

そのおかげで、MySQL は同じテーブルではなく、そのテーブルからフェッチされた結果に対して動作します。動作するはずですが、良い方法ではありません (たとえば、パフォーマンスについて考えてください)。

また、このクエリを使用する前に、https: //dba.stackexchange.com/questions/1371/problem-with-mysql-subquery (@ypercube によるリンク) で考えられる問題について詳しくお読みください。

于 2013-10-22T22:05:22.607 に答える
0

大変お世話になりましたが、先に進まなければならないので、一時テーブルを使用して書き直しました

drop table if exists tmp_cfs_to_nuke;
create temporary table tmp_cfs_to_nuke(
    team_id INT(11) unsigned
);

insert into tmp_cfs_to_nuke 
    select mcfv2.team_id from match_custom_field_team_value mcfv2 
        where mcfv2.match_id=10648 and mcfv2.match_custom_field_id=14917 and mcfv2.event_id=9;

delete from match_custom_field_team_value
where match_id=10648 
  and match_custom_field_id=14917 
  and event_id in (2, 6, 8, 4)
  and team_id in (
    select team_id from tmp_cfs_to_nuke
 );
于 2013-10-23T16:08:01.563 に答える