增加列的值,直到某些条件符合

时间:2016-04-14 14:48:17

标签: mysql

我有一个带this configuration

的MySQL表

我想执行以下操作:

让我们假设一个变量amount = some_value

for each row in ingridients_entry {
IF amount = 0
break;
END IF
IF amount > count - consumed
    SET amount = amount - (count-consumed) AND consumed = count
ELSE
    SET consumed = consumed + amount AND amount = 0
END IF
}

是否可以使用sql UPDATE查询?

我正在考虑在像这样的程序中使用更新案例

DELIMITER $$
CREATE PROCEDURE consumeIngridients(IN amount FLOAT, IN sr INT)
BEGIN
update ingridients_entry set consumed=CASE
WHEN amount = 0 THEN consumed
WHEN amount > (count - consumed) THEN SET amount = amount - (count - consumed); count
ELSE consumed + amount;SET amount = 0;
END CASE;
where serial=sr and count>consumed;
END$$
DELIMITER ;

但它给了我错误:

  

错误1064(42000):您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以便使用接近' SET amount = amount - (count - consume)的正确语法;计算ELSE消耗量+金额; SET amoun'在第5行

View the schema here

请帮忙!

0 个答案:

没有答案