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

前端 未结 6 710
灰色年华
灰色年华 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 16:59

    The correct answer is in this SO post.

    The problem with here accepted answer is - as was already mentioned multiple times - creating a full copy of the whole table. This is way far from optimal and the most space complex one. The idea is to materialize the subset of data used for update only, so in your case it would be like this:

    UPDATE manager as m1
    SET m1.status = 'Y'
    WHERE m1.branch_id IN (
        SELECT * FROM(
            SELECT m2.branch_id FROM manager as m2 
            WHERE (m2.branch_id,m2.year) IN (
                SELECT DISTINCT branch_id,year 
                FROM `branch_master` 
                WHERE type = 'finance')
        ) t
    )
    

    Basically you just encapsulate your previous source for data query inside of

    SELECT * FROM (...) t
    
    0 讨论(0)
  • Try :::

    UPDATE manager as m1 
    SET m1.status = 'Y'
    WHERE m1.branch_id IN (
      (SELECT DISTINCT branch_id
      FROM branch_master
      WHERE type = 'finance'))
      AND m1.year IN ((SELECT DISTINCT year
      FROM branch_master 
      WHERE type = 'finance'))
    
    0 讨论(0)
  • 2020-11-27 17:03

    Try to use the EXISTS operator:

    UPDATE manager as m1 
    SET m1.status = 'Y'
    WHERE EXISTS (SELECT 1
                  FROM (SELECT m2.branch_id             
                        FROM branch_master AS bm
                        JOIN manager AS m2
                        WHERE bm.type = 'finance' AND
                            bm.branch_id = m2.branch_id AND
                            bm.year = m2.year) AS t
                  WHERE t.branch_id = m1.branch_id); 
    

    Note: The query uses an additional nesting level, as proposed by @Thorsten, as a means to circumvent the Table is specified twice error.

    Demo here

    0 讨论(0)
  • 2020-11-27 17:05

    Maybe not a solution, but some thoughts about why it doesn't work in the first place:

    Reading data from a table and also writing data into that same table is somewhat an ill-defined task. In what order should the data be read and written? Should newly written data be considered when reading it back from the same table? MySQL refusing to execute this isn't just because of a limitation, it's because it's not a well-defined task.

    The solutions involving SELECT ... FROM (SELECT * FROM table) AS tmp just dump the entire content of a table into a temporary table, which can then be used in any further outer queries, like for example an update query. This forces the order of operations to be: Select everything first into a temporary table and then use that data (instead of the data from the original table) to do the updates.

    However if the table involved is large, then this temporary copying is going to be incredibly slow. No indexes will ever speed up SELECT * FROM table.

    I might have a slow day today... but isn't the original query identical to this one, which souldn't have any problems?

    UPDATE manager as m1 
    SET m1.status = 'Y'
    WHERE (m1.branch_id, m1.year) IN (
          SELECT DISTINCT branch_id,year 
            FROM `branch_master` 
           WHERE type = 'finance'
        )
    
    0 讨论(0)
  • 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';

    0 讨论(0)
  • 2020-11-27 17:16

    This is a typical MySQL thing and can usually be circumvented by selecting from the table derived, i.e. instead of

    FROM manager AS m2
    

    use

    FROM (select * from manager) AS m2
    

    The complete statement:

    UPDATE manager
    SET status = 'Y'
    WHERE branch_id IN
    (
      select branch_id
      FROM (select * from manager) AS m2
      WHERE (branch_id, year) IN
      (
        SELECT branch_id, year
        FROM branch_master
        WHERE type = 'finance'
      )
    );
    
    0 讨论(0)
提交回复
热议问题