MySQL根据另一个表中的列总和触发更新字段

时间:2015-02-18 09:11:43

标签: mysql triggers

我有两个表'调查'和'结果'

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

我是触发器的新手,我想弄清楚如何在调查表中获取p1q5的总和并将其更新为PY5Q1,其中结果表中的id = 1 任何想法将不胜感激

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

1 个答案:

答案 0 :(得分:0)

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