Skip to content

MySQL: How to swap entires of two columns with just one query

Check this out, I found out a really nice way how to swap values of two columns with just one query, using user defined variables. Given a table with two columns a and b. To swap their values in whole, use this query:

[text]
UPDATE my_table SET a=@tmp:=a, a=b, b=@tmp;
[/text]

Done! Isn’t that easy?