NIl*_*rma 2 mysql sql sql-update
假设我的表格配置文件中有数据字段教育,现在我想更新education ='01',其中早期教育是'BA',同样教育= '02',其中教育是'MD'
所以我可以像这样完成这个任务
update profile set education='01' where education='BA';
update profile set education='02' where education='MD';
Run Code Online (Sandbox Code Playgroud)
我的问题是我可以在一个命令中执行此任务吗?
update profile set education='01' where education='BA' and set education='02' where education='MD';
Run Code Online (Sandbox Code Playgroud)
这种语法有误,请告诉我这是可能的以及如何?如果不可能,请告诉我相关信息......
您可以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
Run Code Online (Sandbox Code Playgroud)