MySQL triggers to update field based on sum of column from another table

随声附和 提交于 2019-12-31 05:42:05

问题


I have two tables 'survey' and 'results'

survey table
-----------------------------------------------
survey_id  | name  | p1q1 | p1q2 | p1q5  
-----------------------------------------------
1          | John  | 10   | 10   | 5 
-----------------------------------------------
2          | Erick | 12   | 15   | 23 
-----------------------------------------------

results table

id  | Uptake | PY5Q1 |
----------------------------
1   | AZT    | NULL  |
----------------------------
2   | UPDP   | NULL  |
----------------------------

I'm new to triggers and am trying to figure out how to get sum of p1q5 in survey table and update it to PY5Q1 where id = 1 in results table Any ideas will be appreciated

CREATE TRIGGER `results` AFTER INSERT ON `survey` FOR EACH ROW BEGIN
UPDATE results SET PY5Q1=(SELECT SUM(p1q5)) WHERE id=1;
END

回答1:


CREATE TRIGGER `results` AFTER INSERT ON `survey` FOR EACH ROW
BEGIN
UPDATE results SET PY5Q1=(SELECT SUM(p1q5)FROM survey) WHERE id=1;
END


来源:https://stackoverflow.com/questions/28579734/mysql-triggers-to-update-field-based-on-sum-of-column-from-another-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!