Is there a way to swap two numeric values that are keyed (unique) in a single update statement? The only way i can think to do it is with three (update the fields to a non-existant key-pair, and then update each individually to put it back in the correct slot)
I eventually struck upon the thought that i could do it in two using the set… case… syntax…because id is a Signed Int field that’s only using it’s positive values.
#1 = UPDATE games SET id = id * -1 WHERE id IN($a,$b); #2 = UPDATE games SET id = CASE WHEN id = ($a * -1) THEN $b WHEN id = ($b * -1) THEN $a ELSE id END WHERE id IN ($a,$b);
A wonderful theory, but i dont see how that solves my problem.
If you tell it to apply transformation on 2 rows, it will apply it to one row at a time - causing a key collision.
The cited theory uses 3 operations, and is therefore not more efficient than the former (and in fact is worse, if R1 XOR R2 exists as a key already - cause it’ll collide then)