Here is a pure SQL solution, which works only if the column being swapped is not a unique key.
mysql> select * from swaptable; +----+------+ | id | num | +----+------+ | 5 | 100 | | 6 | 200 | | 7 | 300 | +----+------+ 3 rows in set (0.00 sec) mysql> UPDATE swaptable SET id = CASE id WHEN 5 THEN 6 -> WHEN 6 THEN 5 END -> WHERE id IN (5,6); Query OK, 2 rows affected (0.01 sec) Rows matched: 2 Changed: 2 Warnings: 0 mysql> select * from swaptable; +----+------+ | id | num | +----+------+ | 6 | 100 | | 5 | 200 | | 7 | 300 | +----+------+ 3 rows in set (0.00 sec) mysql> UPDATE swaptable SET id = CASE id WHEN 5 THEN 6 -> WHEN 6 THEN 5 END -> WHERE id IN (5,6); Query OK, 2 rows affected (0.01 sec) Rows matched: 2 Changed: 2 Warnings: 0 mysql> select * from swaptable; +----+------+ | id | num | +----+------+ | 5 | 100 | | 6 | 200 | | 7 | 300 | +----+------+ 3 rows in set (0.00 sec)
In reply to Re: non-perl SQL question
by dbwiz
in thread non-perl SQL question
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |