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.
Something like this should do it:
UPDATE `mytable1`
JOIN `mytable2` ON `mytable1`.`state` = `mytable2`.`state`
AND
(
(`mytable1`.`a` = `mytable2`.`a` AND `mytable1`.`b` != `mytable2`.`b`) OR
(`mytable1`.`b` = `mytable2`.`b` AND `mytable`.`a` != `mytable2`.`a`)
)
SET `mytable1`.`state` = 5, `mytable2`.`state` = 5
WHERE `mytable1`.`state` = 0;
Here's what I came up with:
update mytable1 t1 inner join mytable2 t2 on (t1.a=t2.a or t1.b=t2.b or t2.c=t1.c) set t1.state=5,t2.state=5 where((cast(t1.a=t2.a as unsigned integer) + (cast(t1.b=t2.b as unsigned integer) + (cast(t1.c=t2.c as unsigned integer)) <3) and t1.state=0;
Yes, max 2 values could be equal, only 1 should be different.
It looks like it works, and it's fast. Any comment?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.