Will SQL update affect its subquery during the update run?

后端 未结 4 752
挽巷
挽巷 2020-12-30 11:15

I\'m just composing a complex update query which looks more or less like this:

update table join
    (select y, min(x) as MinX 
     from table
     group by         


        
4条回答
  •  轮回少年
    2020-12-30 11:24

    Although its been noted you SHOULDN'T be able to do an update to a table based on its own data, you should be able to adjust the MySQL syntax to allow for it via

    update Table1, 
           (select T2.y, MIN( T2.x ) as MinX from Table1 T2 group by T2.y ) PreQuery
      set Table1.x = Table1.x - PreQuery.MinX
      where Table1.y = PreQuery.y
    

    I don't know if the syntax goes a different route using JOIN vs the comma list version, but by the complete prequery you do would have to be applied first for its result completed ONCE, and joined (via the WHERE) to actually perform the update.

提交回复
热议问题