假设我的表中有一个数据字段教育,现在我想更新教育=‘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';
这个语法是错误的,请告诉我这是可能的,怎么可能的?如果是不可能的,也请让我知道.
发布于 2012-09-16 13:39:45
您可以在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
https://stackoverflow.com/questions/12447306
复制相似问题