Problem: I’d like to update rows that don’t match a left join (or another fast solution).
Initial goal: Update records of mytable1 that have state=0 and has either (XOR!) column “a” that matches mytable2, or column “b” that matches mytable2 (“a” and “b” should not both match!). Set the records of both tables to state=5.
I tried (and failed):
update mytable1 as t1
left join mytable2 as t2 on (t1.a=t2.a and t1.b=t2.b)
set t1.state=5,t2.state=5
where t1.state=0 and t2.state=0 and t2.a is null;
As you can see I tried to join all records that match BOTH values, so that I could update records that don’t match, also updating the rows that didn’t match in mytable2. Rows from mytable1 get updated but not those from table2. I could update the rows that DO match the left join, but that’s 99% of the rows which would be a performance hit I think (I did ask this question so I never got to compare ;).
Thank you for your time.
Here’s what I came up with:
Yes, max 2 values could be equal, only 1 should be different.
It looks like it works, and it’s fast. Any comment?