Consider I have two tables, t1
and t2
. t1
has two columns: c1
and c2
. t2
has two columns: c1
and c3
. We insert values in both tables:
CREATE TABLE t1 (c1 INT, c2 INT);
CREATE TABLE t2 (c1 int, c3 int);
INSERT INTO t1 VALUES (10,10), (20,20);
INSERT INTO t2 VALUES (10,10), (20,20);
Updating t1
with this query:
update t1
set c2 = c2 + 10,
c2 = c2 + 20
will yield the next result: c2
column will respectively be equal to 40,50. This means, the first update was evaluated and then the next update used the first update value, so it's like using c2 = c2 + 10 + 20
.
Considering I need to update t1
again, but this time, joining with t2
table, using this query:
update t1 left join t2 using (c1)
set c2 = c2+10,
c2 = c2+20;
This query will yield the next result: c2
column will respectively be equal to 60,70, which means, the first update was ignored, and only the c2 = c2+20
was being evaluated at the end. I was expecting the results to be 70,80.
I know that I didn't use t2
in the join, but in my real example I need to use t2
and I need to update same column twice, using the first update value.
Why does the same update with join yield different result, and is it possible to make it work without splitting the update into two separate updates?
EDIT: I've been told that the same code used to work on MariaDB 10.1, but after upgrading to 10.3 it failed. (Unfortunately, I can't find an online MariaDB 10.1 fiddle to test it myself)