SQL:设置触发器以获取行总和
这是结果表:
student_id | subject_id | get_ca1 | get_ca2 | get_exam | get_total
----------------------------------------------------------------------
101 | 1 | 10 | 7 | 10 |
102 | 2 | 5 | 5 | 10 |
103 | 1 | 9 | 10 | 4 |
101 | 1 | 8 | 10 | 10 |
103 | 2 | 2 | 10 | 10 |
104 | 1 | 7 | 8 | 5 |
101 | 2 | 7 | 8 | 5 |
我想创建一个触发器,该触发器将总结get_ca1 + get_ca2 ... + get_exam
并将总计存储在get_total
column中。 插入行get_ca1,get_ca2,... get_exam
时,触发器应计算总数并存储在get_total
列中。
我今天刚刚了解到触发因素,所以我一点都不了解。但是到目前为止,这就是我尝试的,当然也丢了一个错误。
CREATE TRIGGER `sum total ` AFTER INSERT ON `exam_group_exam_results`
FOR EACH ROW SET get_tot_score = (get_ca1 + get_ca2 + get_ca3 + get_ca4 + get_ca5 + get_ca6 + get_exam);
This is the results table:
student_id | subject_id | get_ca1 | get_ca2 | get_exam | get_total
----------------------------------------------------------------------
101 | 1 | 10 | 7 | 10 |
102 | 2 | 5 | 5 | 10 |
103 | 1 | 9 | 10 | 4 |
101 | 1 | 8 | 10 | 10 |
103 | 2 | 2 | 10 | 10 |
104 | 1 | 7 | 8 | 5 |
101 | 2 | 7 | 8 | 5 |
I want to create a trigger that will sum up the rows get_ca1 + get_ca2 ...+ get_exam
and store the total in the get_total
column.
When the rows get_ca1, get_ca2,... get_exam
are inserted, the trigger should calculate the total and store in the get_total
column.
I just learnt about triggers today so I'm not knowledgeable in it at all. But so far, this is what I tried and it of course threw an error.
CREATE TRIGGER `sum total ` AFTER INSERT ON `exam_group_exam_results`
FOR EACH ROW SET get_tot_score = (get_ca1 + get_ca2 + get_ca3 + get_ca4 + get_ca5 + get_ca6 + get_exam);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MySQL不知道您的列在哪里Belog,因为您缺少对新行的引用。
另外,您只能在插入行之前更改
值
Mysql doesn't know where your columns belog because you are missing a reference to the NEW row.
Also you can only change values, before the row was inserted
so use