【问题标题】:Trigger updating a table after insert [duplicate]插入后触发更新表[重复]
【发布时间】:2021-05-05 04:15:27
【问题描述】:
CREATE TRIGGER generate_cost
    AFTER INSERT on order_products FOR EACH ROW BEGIN
        SET @cost = (select price from products where products.id = NEW.product_id);
        UPDATE order_products set order_products.cost = @cost * NEW.quantity;
END;

我确实需要动态生成 order_items 的成本,因为从产品中获取价格并将其乘以客户输入的数量,但是它不起作用 - 我该怎么办

它说 - #1442 - 无法更新存储函数/触发器中的表“order_products”,因为它已被调用此存储函数/触发器的语句使用

【问题讨论】:

标签: mysql sql triggers mariadb


【解决方案1】:

如果要更改定义触发器的表中的值,请使用before insert 触发器:

CREATE TRIGGER generate_cost
    BEFORE INSERT on order_products
    FOR EACH ROW
BEGIN
    select new.cost := p.price * new.quantity
    from products p
    where p.id = NEW.product_id;
END;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-08
    • 2016-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多