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

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

提交回复
热议问题