Table 单列更新后触发

Table trigger after update on single column

我对在 SQL 中使用触发器还很陌生,似乎找不到任何与我希望的方向相同的主题教程。

我有以下 table:

id  int(11) AI PK
user_id int(11)
academy_id  int(11)
module_id   int(11)
module_type_id  int(11)
team_id int(11)
score   int(11)
medal_id    int(11) PK
timestamp   datetime
possible_correct    int(11)
user_correct    int(11)

现在我的目标是在此 table 上创建一个 AFTER UPDATE 触发器,以便在更新完成后使用以下内容重新计算 score 列 (possible_correct / user_correct )

有谁知道我怎样才能做到这一点?

您无法使用触发器 after update 更新相同的 table,您需要 before update 并将值设置为

delimiter //
create trigger `your_trigger_name` before update on `your_table_name`
for each row
begin
 if new.possible_correct > 0 and new.user_correct > 0 then
   set new.score = new.possible_correct / new.user_correct ;
 end if ;
end;//

delimiter ;