这是我的表:
// mytable
+----+------+
| id | num |
+----+------+
| 1 | 400 |
| 2 | 200 |
| 3 | 200 |
| 4 | 250 |
+----+------+
这是预期的结果:
+----+------+
| id | num |
+----+------+
| 1 | 390 |
| 2 | 200 |
| 3 | 220 |
| 4 | 250 |
+----+------+
如您所见,我已将10
减去id = 1
,我已添加20
id = 3
这是我的问题:
UPDATE mytable SET num = num +/- (CASE WHEN num = 1 THEN 10 WHEN num = 3 THEN 20 END)
WHERE id IN (1,3)
如何管理+ or -
?
答案 0 :(得分:9)
只需在案例陈述中使用负数:
UPDATE mytable
SET num = num + CASE WHEN id = 1 THEN -10 ELSE 20 END
WHERE id IN (1,3)