how to write mysql trigger

前端 未结 2 407
我在风中等你
我在风中等你 2021-01-15 03:15


there are two tables
table A

if user use this query \"insert into A values(abc,1,50);\"

then trigger should check student_nam

相关标签:
2条回答
  • 2021-01-15 03:30

    try like this...

    if (Select Count(Name) from B where B.Name=new.name)<1 
    insert into summary_score (name,number,all_marks)
    values(new.name,new.marks,new.score);
    else 
    update B set all_marks=old.all_marks+new.student_marks where B.Name=new.Name;
    
    0 讨论(0)
  • 2021-01-15 03:35

    You can simply this by using On Duplicate Key Update . Add a unique key on student name / ID then use the following code in your trigger

    begin
       insert into summary_score (name,number,all_marks)
       values(new.name,new.marks,new.score)
       on duplicate key update all_marks=old.all_marks+new.student_marks
      where B.Name=new.Name;
    end $$
    
    0 讨论(0)
提交回复
热议问题