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

2009 June 24
by tobi

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:

UPDATE my_table SET a=@tmp:=a, a=b, b=@tmp;

Done! Isn’t that easy?

No comments yet

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS