Table is specified twice, both as a target for 'UPDATE' and as a separate source for data in mysql

前端 未结 6 740
灰色年华
灰色年华 2020-11-27 16:42

I have below query in mysql where I want to check if branch id and year of finance type from branch_master

6条回答
  •  臣服心动
    2020-11-27 17:13

    The problem I had with the accepted answer is that create a copy of the whole table, and for me wasn't an option, I tried to execute it but after several hours I had to cancel it.

    A very fast way if you have a huge amount of data is create a temporary table:

    1. Create TMP table

      CREATE TEMPORARY TABLE tmp_manager (branch_id bigint auto_increment primary key, year datetime null);

    2. Populate TMP table

      insert into tmp_manager (branch_id, year) select branch_id, year from manager;

    3. Update with join

      UPDATE manager as m, tmp_manager as tmp_m inner JOIN manager as man on tmp_m.branch_id = man.branch_id SET status = 'Y' WHERE m.branch_id = tmp_m.branch_id and m.year = tmp_m.year and m.type = 'finance';

提交回复
热议问题