通过使用CASE
语句更新我的SQL表,我遇到了问题。
SELECT number,amount,minimuminventory FROM artikel WHERE number=17;
+--------+--------+------------------+ | number | amount | minimuminventory | +--------+--------+------------------+ | 17 | 10 | 0 | +--------+--------+------------------+
我的金额为10但是当我更新我的桌子时:
UPDATE artikel
SET amount = CASE WHEN amount - minimuminventory - 2 < 0
THEN amount=amount-2
ELSE amount=99
END
WHERE artnr=17;
查询OK,1行受影响(0,01秒)
匹配的行数:1已更改:1警告:0
mysql将我的表更新为0
SELECT number,amount,minimuminventory FROM artikel WHERE number=17;
+--------+--------+------------------+ | number | amount | minimuminventory | +--------+--------+------------------+ | 17 | 0 | 0 | +--------+--------+------------------+
你看到了问题吗?
答案 0 :(得分:6)
amount = CASE WHEN amount - minimuminventory - 2 < 0 THEN amount=amount-2 ELSE amount=99 END
CASE语句中的值是amount
列的预期值,但您在其THEN和ELSE部分内再次执行amount=x
。将其更改为:
amount = CASE WHEN amount - minimuminventory - 2 < 0 THEN amount-2 ELSE 99 END
答案 1 :(得分:0)
你看到了问题吗?
在你的陈述中:
UPDATE artikel
SET amount = CASE WHEN amount - minimuminventory - 2 < 0
THEN amount=amount-2
ELSE amount=99
END
WHERE artnr=17;
比较被解释为布尔值而不是赋值。
SET amount = (amount=amount-2) -- comparison as if amount is equal to (17=17-2)
SET amount = (amount=99) -- comparison as if amount is equal to (17=99)
SET amount = false -- i.e. 0, because 17 != 15 or 17 != 99
这导致0
列中的值amount
。
更改您的更新声明,如下所示:
UPDATE artikel
SET amount = CASE WHEN amount - minimuminventory - 2 < 0
THEN amount-2
ELSE 99
END
WHERE artnr=17;