如何交换记录值?

时间:2012-10-03 15:23:06

标签: sql

如何将role = 1的所有记录更新为2,将role = 2的记录更新为值1

  login   | role
----------+------
 john     |    1
 anne     |    2
 adam     |    1
 robert   |    1
 katie    |    1
 bob      |    2
 tom      |    2
 ...many records with 1 and 2 values

2 个答案:

答案 0 :(得分:4)

UPDATE table

SET role = CASE role
               WHEN 1 THEN 2
               ELSE 1
           END

WHERE role = 1 OR role = 2;

答案 1 :(得分:1)

UPDATE [table]
SET role = CASE role
               WHEN 1 THEN 2
               ELSE 1
           END
WHERE role = 1 OR role = 2;