How to re-assign two variables in a conditional UPDATE
Question
Consider a simple update as
SET @var1 = 20;
SET @var2 = 26;
UPDATE table1 SET
col1=IF(@var1>50, @var1 := @var1-col4, @var1 := @var1+col5),
col2=IF(@var2>50, @var2 := @var2-col6, @var2);
How we can change two variables in one condition. For example, consider that when @var1<50
, we want to change not only @var1 := @var1+col5
, but also re-assign @var2 := @var2 + 100
. In fact, regardless of the condition 2, we want to increase @var2
, if the first condition fails (the second part).
Is it possible to re-assign two user-variable in a IF STATEMENT
or we need to add another IF STATEMENT
?
This will be equivalent to
SET @var1 = 20;
SET @var2 = 26;
UPDATE table1 SET
col1=IF(@var1>50, @var1 := @var1-col4, @var1 := @var1+col5),
virtual_col=IF(@var1>50, NULL, @var2 := @var2+100),
col2=IF(@var2>50, @var2 := @var2-col6, @var2);
Here we have created a useless column, not to collect the data, but to repeat the IF STATEMENT
for the @var2
. Obviously, it is not good from performance point of view, as we are running a useless UPDATE
action.
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange