MySQL UPDATE CASE没有按预期工作

时间:2014-08-11 17:53:17

标签: php mysql sql-update

我正在尝试使此查询有效,但我无法更新所需的字段。这是我的代码:

UPDATE inventory
SET inventoryCount = CASE 
 WHEN itemId = 1 THEN inventoryCount+1
 WHEN itemId = 2 THEN inventoryCount+3
 WHEN itemId = 3 THEN inventoryCount+6
 ELSE inventoryCount
 END
WHERE itemId IN (1,2,3) AND outletId = 23 AND companyId = 2

我需要将X金额加到inventoryCount中的当前金额,其中一些似乎忽略它并且没有任何变化。

我做错了吗?

1 个答案:

答案 0 :(得分:3)

您的查询有语法错误,因此可能无法正确执行;

WHEN itemId = 1 THEN inventoryCount+1,  <-- comma should not be there
WHEN itemId = 2 THEN inventoryCount+3,  <-- comma should not be there
WHEN itemId = 3 THEN inventoryCount+6,  <-- comma should not be there

如果删除多余的逗号,查询将正确执行;

An SQLfiddle to show the working query