Forum Moderators: open

Message Too Old, No Replies

SQL to swap culomn values?

MySQL cloumn swap

         

erikcw

7:07 pm on Mar 26, 2007 (gmt 0)

10+ Year Member



Hi,

I'm trying to swap the contents of some columns in a row.

UPDATE data SET backup=current, bkcount=count, current=backup, count=bkcount WHERE id=123

Unfortunately, this sets both columns to the same value. What is the correct way to do this without resulting to multiple queries?

Thanks!

Clark

8:18 pm on Mar 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Haven't used multiple table aliases very often but I think this is a case where it is used so I'll give you some code to try and report back.

Try (after testing on a backup of course):

UPDATE data d, data2 d2,
SET
d.backup=d2.current,
d.bkcount=d2.count,
d.current=d2.backup,
d.count=d2.bkcount
WHERE
d.id=d2.id
AND
d.id=123