updaten some_table set one_field = xxx,another_field =(仅在条件下更新,例如大于零然后减1)。
答案 0 :(得分:9)
使用CASE将字段更新为新值,或者根据条件的值再次设置相同的值。
UPDATE some_table
SET one_field = 'xxx',
another_field = CASE WHEN another_field > 0
THEN another_field - 1
ELSE another_field
END
对于递减不允许低于零的数字,您可以使用GREATEST:
UPDATE some_table
SET one_field = 'xxx',
another_field = GREATEST(0, another_field - 1)
答案 1 :(得分:1)
update sone_table set one_field=xxx, another_field=IF(another_field > 0, value , another_field)
答案 2 :(得分:0)
在您的程序中使用IF / Then / Else:
CREATE PROCEDURE UpdateTable(IN VALUE1 INT, IN VALUE2 INT)
BEGIN
IF (VALUE2 > 0)
THEN
UPDATE SOME_TABLE SET ONE_FIELD=VALUE1, ANOTHER_FIELD=VALUE2;
ELSE
UPDATE SOME_TABLE SET ONE_FIELD=VALUE1;
END IF;
END|