SQL:设置触发器以获取行总和

发布于 2025-02-08 17:57:10 字数 1057 浏览 0 评论 0原文

这是结果表:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蒲公英的约定 2025-02-15 17:57:10

MySQL不知道您的列在哪里Belog,因为您缺少对新行的引用。

另外,您只能在插入行之前更改

CREATE TRIGGER `sum_total_before_INSERT` BEFORE INSERT ON `exam_group_exam_results`
FOR EACH ROW 
SET NEW.get_tot_score = (NEW.get_ca1 + NEW.get_ca2 + NEW.get_ca3 + NEW.get_ca4 + NEW.get_ca5 + NEW.get_ca6 + NEW.get_exam);

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

CREATE TRIGGER `sum_total_before_INSERT` BEFORE INSERT ON `exam_group_exam_results`
FOR EACH ROW 
SET NEW.get_tot_score = (NEW.get_ca1 + NEW.get_ca2 + NEW.get_ca3 + NEW.get_ca4 + NEW.get_ca5 + NEW.get_ca6 + NEW.get_exam);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文