2

假设我的表配置文件中有一个数据字段 education,现在我想更新 education='01' 早期教育是 'BA' ,类似地 education='02' 教育是 'MD'

所以我可以像这样完成这个任务

update profile set education='01' where education='BA';
update profile set education='02' where education='MD';

我的问题是我可以只用一个命令来完成这项任务吗

   update profile set education='01' where education='BA' and set education='02' where education='MD';

这种语法是错误的,请告诉我这是否可能以及如何?如果不可能,也请让我知道...

4

1 回答 1

5

您可以CASESET子句中使用语句,但要小心包含一个ELSE将列设置为其当前值的 case——否则,两个 case 不匹配的行将被设置为NULL.

UPDATE profile
SET education = 
  CASE
    WHEN education = 'BA' THEN '01'
    WHEN education = 'MD' THEN '02'
    /* MUST include an ELSE case to set to current value, 
       otherwise the non-matching will be NULLed! */
    ELSE education
  END
于 2012-09-16T13:39:45.080 回答