假设我的表格配置文件中有数据字段教育,现在我想更新education ='01',其中早期教育是'BA',类似教育= '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';
这种语法错了,请告诉我这是可能的以及如何? 如果不可能,请告诉我相关信息......
答案 0 :(得分:5)
您可以在CASE
子句中使用SET
语句,但要小心包含一个ELSE
个案例,该列将列设置为其当前值 - 否则,这些行是两种情况匹配的将设置为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