mysqldude

Update rows reversing column values

Version compatibility

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.8
SET f2 = f1, f1 = f2 does not swap (original)
original post
✓✓✓✓✓✓✓✓✓
Swap via session variables f1=@f1:=f1, f1=@f2:=f2, f2=@f1 (original)
original post
✓✓⚠ 1287⚠ 1287⚠ 1287✓✓✓✓
Swap through a self-join (no variables)
modern rewrite
✓✓✓✓✓✓✓✓✓
MariaDB sql_mode SIMULTANEOUS_ASSIGNMENT makes SET f1 = f2, f2 = f1 swap
version check
✗ 1231✗ 1231✗ 1231✗ 1231✗ 1231✓✓✓✓
SET f2 = f1, f1 = f2 does not swap (original) original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
Swap via session variables f1=@f1:=f1, f1=@f2:=f2, f2=@f1 (original) original post
MySQL 5.6✓5.7✓8.0⚠ 12878.4⚠ 12879⚠ 1287
MariaDB 10.6✓10.11✓11.4✓11.8✓
Swap through a self-join (no variables) modern rewrite
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
MariaDB sql_mode SIMULTANEOUS_ASSIGNMENT makes SET f1 = f2, f2 = f1 swap version check
MySQL 5.6✗ 12315.7✗ 12318.0✗ 12318.4✗ 12319✗ 1231
MariaDB 10.6✓10.11✓11.4✓11.8✓
✓works ✓ 1062fails with the error the step demonstrates ⚠works with a warning ≠runs, result differs ✗fails Tested 2026-11-25 on 5.6.51, 5.7.44, 8.0.46, 8.4.13, 9.7.4, 10.6.28, 10.11.19, 11.4.13, 11.8.9
What the warnings and failures say (2)
  • Swap via session variables f1=@f1:=f1, f1=@f2:=f2, f2=@f1 (original) (MySQL 8.0, MySQL 8.4, MySQL 9): warning 1287: Setting user variables within expressions is deprecated and will be removed in a future release. Consider alternatives: 'SET variable=expres
  • MariaDB sql_mode SIMULTANEOUS_ASSIGNMENT makes SET f1 = f2, f2 = f1 swap (MySQL 5.6, MySQL 5.7, MySQL 8.0, MySQL 8.4, MySQL 9): error 1231: Variable 'sql_mode' can't be set to the value of 'SIMULTANEOUS_ASSIGNMENT'

Related errors: 1287

The original article

First published 2012-04-30. The text below is the original; the table above shows how it behaves on current versions.

I have seen this requested many times and MySQL offers sessions variables which allows values to be temporarily stored. This is extremely handy when a table consists of columns where the values in each of the fields need to be changed. For example, let us imagine we have a table with fields f1 and f2. The value stored in f1 should have been stored in f2 and vice versa. How can we resolve this?

If we go down the road of a traditional UPDATE statement we would see the following:

mysql> select * from updatefields;
+------+------+
| f1   | f2   |
+------+------+
|    0 |    1 |
|    2 |    3 |
|    3 |    4 |
+------+------+
3 rows in set (0.00 sec)

mysql> update updatefields set f2 = f1, f1 = f2;
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> select * from updatefields;
+------+------+
| f1   | f2   |
+------+------+
|    0 |    0 |
|    2 |    2 |
|    3 |    3 |
+------+------+
3 rows in set (0.01 sec)

The problem in this case is that the value f1 was updated into field f2 and f2’s new value updates f1. What we want in this case is to temporarily store the f1 value so that it can then be used later on to update the f2 field.

mysql> select * from updatefields;
+------+------+
| f1   | f2   |
+------+------+
|    0 |    1 |
|    2 |    3 |
|    3 |    4 |
+------+------+
3 rows in set (0.00 sec)

mysql> update updatefields set f1=@f1:=f1, f1=@f2:=f2, f2=@f1;
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> select * from updatefields;
+------+------+
| f1   | f2   |
+------+------+
|    1 |    0 |
|    3 |    2 |
|    4 |    3 |
+------+------+
3 rows in set (0.00 sec)

In this case here what we have done is to assign to session variable @f1 the value of f1 which updates the field f1. This is necessary otherwise the operation would fail. Once we have store the field value f1 in @f1 we can then perform the update f1 with the value of f2 and then f2 with the temporarily stored value in @f1. [googleplus]