Skip to content

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?

2 Responses
  1. manoj permalink
    August 29, 2012

    very helpful

  2. algi permalink
    November 2, 2010

    That is great! Loved it.

Comments are closed.