MySQL - UPDATE query with SET statement dependent on the outcome of the previous SET statement

后端 未结 2 578
余生分开走
余生分开走 2021-01-23 09:10

Here is a tabular representation of what I would like to achieve with an UPDATE statement.

+----+----+---+---+----+----------+---------------+---------------+
|          


        
相关标签:
2条回答
  • 2021-01-23 09:25

    Just do the calculations independently:

    update [EXAMPLE]
    set [Calc A] = A - B,
        [Calc B] = (A - B) / D,
        [Calc C] = B / ((A - B) / D)
    
    0 讨论(0)
  • 2021-01-23 09:28

    I actually found a solution to this problem using local variables in the SET statements. See below.

    UPDATE [EXAMPLE]
    SET [Calc A]    = @calc_a : = A - B
        , [Calc B]  = @calc_b := @calc_a / D
        , [Calc C]  = B / @calc_b
    
    0 讨论(0)
提交回复
热议问题