mysql - 从另一个表的聚合中触发更新其他表
问题描述
我正在尝试在 MySql 上触发。
Cenario:我有 3 张桌子:
- com_proposals
- com_proposals_items
- com_proposals_subitems
重要提示:三个表都有此列 (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)
解决方案
推荐阅读
- java - Java URLClassLoader 在 Eclipse 中有效,但在 cmd 中无效
- web-scraping - Beautiful Soup 仅返回 txt 文件的最后一个 URL
- java - 在 Spring MVC 中安装 Spring Security 以启用 CSRF 保护
- python - 将具有相同值的项目分组,并将该值作为标题
- javascript - Express.js 搜索表单未从 api 返回响应
- ios - 如何在 iOS 主屏幕小部件中显示 WkWebView 屏幕截图?
- python - 将数据框的列附加到列表中
- bash - 我使用“-print0”选项来处理带空格的文件名,但出现错误
- javascript - 如何修复字符串保存为代码到单词?
- c++ - 将函数指针 reinterpret_cast 与 void(*)() 进行比较是未定义的行为吗?