您好我正在开设一个有各种学期的大学成绩系统。我有这样的表结构: -
+------+------+------+------+------+------+------+------+------+
| sem1 | sem2 | sem3 | sem4 | sem5 | sem6 | sem7 | sem8 | cgpa |
+------+------+------+------+------+------+------+------+------+
| | | | | | | | | |
+------+------+------+------+------+------+------+------+------+
我想设计一个触发器,以便cgpa列由学期中的值自动计算。当sem 1中只有一个值时,则cgpa = sem1,如果sem1和sem2的值不为null,则cgpa =(sem1 + sem2)/ 2,如果sem1,sem2和sem3的值不为null,则cgpa =(sem1) + sem2 + sem3)/ 3等等其他条件。
我是触发器概念的新手,无法想出正确的方法。下面给出的代码是我尝试过的,我知道这不起作用: -
CREATE DEFINER=`root`@`localhost` TRIGGER `ABC` BEFORE UPDATE ON `cgpa` FOR EACH ROW BEGIN
SET
NEW.cgpa = NEW.sem1;
SET
NEW.cgpa = (NEW.sem1 + NEW.sem2) / 2;
SET
NEW.cgpa = (NEW.sem1 + NEW.sem2 + NEW.sem3) / 3;
SET
NEW.cgpa = (NEW.sem1 + NEW.sem2 + NEW.sem3 + NEW.sem4) / 4;
SET
NEW.cgpa = (NEW.sem1 + NEW.sem2 + NEW.sem3 + NEW.sem4 + NEW.sem5) / 5;
SET
NEW.cgpa = (NEW.sem1 + NEW.sem2 + NEW.sem3 + NEW.sem4 + NEW.sem5 + NEW.sem6) / 6;
SET
NEW.cgpa = (NEW.sem1 + NEW.sem2 + NEW.sem3 + NEW.sem4 + NEW.sem5 + NEW.sem6 + NEW.sem7) / 7;
SET
NEW.cgpa = (NEW.sem1 + NEW.sem2 + NEW.sem3 + NEW.sem4 + NEW.sem5 + NEW.sem6 + NEW.sem7 + NEW.sem8) / 8;
END
我是触发器的新手,所以我无法想办法。如果有人能帮助我这样做,我将非常感激。任何帮助都非常感谢。
答案 0 :(得分:0)
尝试将sem1,sem2,sem3,sem4,sem5值初始化为0,并在所有情况下执行求和