Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: non-perl SQL question

by dbwiz (Curate)
on Dec 31, 2003 at 12:55 UTC ( [id://317875]=note: print w/replies, xml ) Need Help??


in reply to non-perl SQL question

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)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://317875]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2024-04-20 10:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found