首页 > 解决方案 > 从另一个表的聚合中触发更新其他表

问题描述

我正在尝试在 MySql 上触发。

Cenario:我有 3 张桌子:

重要提示:三个表都有此列 (value_gross,value_disc_auto,value_disc_comercial) 并且此值的来源与最后一个不同 - com_proposals_subitems

如果我更新第二个表中的数量,我必须更新主表(com_proposals),这需要第三个表(..subitems)的计算(总和)。看:

DELIMITER $$

CREATE TRIGGER trigger_update_com_proposals_cart_items_updatevalues 
AFTER UPDATE ON com_proposals_cart_items
FOR EACH ROW
BEGIN 
    SET @id = OLD.id;
    SET @prop_id = OLD.proposal_id;
    
    SELECT
        @gross := sum(value_gross),
        @auto := sum(value_disc_auto),
        @com := sum(value_disc_comercial)
    from
        com_proposals_cart_subitems
    where
        cart_item_id = @id;
        
    OLD.value_gross = @gross;
    OLD.value_disc_auto = @auto;
    OLD.value_disc_comercial = @com;
    
    
    SELECT
        @item_gross := sum(value_gross),
        @item_auto := sum(value_disc_auto),
        @item_com := sum(value_disc_comercial)
    from
        com_proposals_cart_items
    where
        id = @id;
    
    UPDATE com_proposals set 
        value_gross = @item_gross,
        value_disc_auto = @item_autoy,
        value_disc_comercial = @item_com
    WHERE id = @prop_id;
END$$    

DELIMITER ;

但是,我收到语法错误:

Error occurred during SQL script execution

Razão:
 SQL Error [1064] [42000]: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.value_gross = @gross;

    OLD.value_disc_auto = @auto;

    OLD.value_disc_comercial' at line 17

有人知道怎么了??多谢!!!服务器版本:5.7.30-0ubuntu0.18.04.1(Ubuntu)

标签: mysqltriggers

解决方案


推荐阅读