问题
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