I’m using MySQL InnoDB.
I’d like to update a table and control the sequence that each column is updated. All in one query if possible. The reason is that I have a queue system for a game. When you finish queue_1 I want it to be assigned the value of queue_2, then erase queue_2.
Right now I’m getting unpredictable results from this. Sometimes total_price_2 is set to zero, THEN loaded into total_price_1. Both becoming zero. (Not what I want.)
I read that the DB decides what order to run the updates. If I have to do two updates that’s fine. My goal is performance.
UPDATE
queue
SET
queue_1 = queue_2,
total_price_1 = total_price_2,
total_wait_1 = total_wait_2,
queue_2 = '',
total_price_2 = 0,
total_wait_2 = 0
WHERE id IN(1,2,3)
Check that
total_price_2wasn’t0before the update, as suggested by MichaelRushton.In MySQL, single-table
UPDATEqueries are processed from ‘left to right’, which is stated in theUPDATEstatement documentation:Specifically, note this example:
Side Note
As a bit of a side note, you can also explicitly specify the order for rows to be updated by using the
ORDER BYclause: