我有一个MySQL更新语句,该语句试图重置两列:label和PersTrCode。 (这是在Coldfusion程序中。)我正在使用CASE语句执行此操作,但似乎语法不正确-我一直收到错误消息。编码:
<cfquery name = 'nonull' datasource = "Moxart">
update FinAggDb
set Label = CASE
WHEN PersActIncOutg = 'I' && PersTrCode IS NULL THEN 'Total Income'
WHEN PersActIncOutg = 'O' && PersTrCode IS NULL THEN 'Total Expense'
WHEN PersActIncOutg IS NULL && PersTrCode IS NULL THEN ' '
ELSE PersTrCode
END
SET PersTrCode = CASE
WHEN PersTrCode IS NULL THEN 'Total'
ELSE PersTrCode
END
</cfquery>
错误是通常的信息性声明:
是否不允许使用多个
CASE
语句?或者有人可以告诉我如何解决此问题? 最佳答案
一个更新语句只有一个set
子句,您要更新的各个列用逗号分隔。另外,请注意,使用and
而不是&&
更为常见,尽管两者均在MySQL中有效:
update FinAggDb
set Label = CASE
WHEN PersActIncOutg = 'I' AND PersTrCode IS NULL THEN 'Total Income'
WHEN PersActIncOutg = 'O' AND PersTrCode IS NULL THEN 'Total Expense'
WHEN PersActIncOutg IS NULL AND PersTrCode IS NULL THEN ' '
ELSE PersTrCode
END
, -- comma here, not a second "set" clause
PersTrCode = CASE
WHEN PersTrCode IS NULL THEN 'Total'
ELSE PersTrCode
END