The key is the ":=" operators. MySQL User Variable
You can also assign a value to a user variable in statements other
than SET. In this case, the assignment operator must be := and not =
because the latter is treated as the comparison operator = in non-SET
statements:
1 Use the one of the updating column
SET @tempVariable := 0;
UPDATE myTable
SET col1 = 5,
col2 = @tempVariable := 100,
col3 = @tempVariable := col2 + 1;
@tempVariable is always 100 and col3 will be always 101. Seems mySQL will use the new assigned value instead of original value in the table. This is different from MS SQL. To make it more clear, try the following example, the value will be 1001 for col3 and @tempVariable.
UPDATE myTable
SET col1 = 5,
col2 = @tempVariable := 100,
col2 = 1000
col3 = @tempVariable := col2 + 1;
2 Use other column in the table than the updating column.
UPDATE myTable
SET col1 = 5,
col2 = @tempVariable := 100,
col3 = @tempVariable := col4 + 1;
@tempVariable and col3 will have the same value. They will be the col4 original value + 1.