MySQL 下创建触发器

mysql触发器的使用和oracle mssql不同,下面的例子中创建触发器的格式、变量
的设定、插入时获取insert值。此触发器是开发中的实际例子,运用时结合自己情
况,可作为参考

create trigger 对医生评价  after insert on tb_estimate_detail FOR EACH ROW
begin
declare replay_number int;
declare praise_number int;
declare n_number int;
declare n_altitueNumber int;
declare n_professional int;
declare n_altitue int;
declare professiona int;
declare altitu int;
set replay_number =(select count(*) from ( select freeaskId from tb_freeask_reply  
where account=new.receiver GROUP BY freeaskId)a);
set praise_number=(select SUM(evaluate) from tb_estimate_detail where 1=1 and 
receiver=@receiver and evaluate=1);
case when replay_number<0 
   then
    set praise_number=0;
  else
   set praise_number=(praise_number/replay_number);
end case;
set n_number=(select count(*)  from tb_estimate_detail where 1=1 and receiver=new.receiver);
set n_professional=(select sum(professional) from tb_estimate_detail 
where 1=1 and receiver=new.receiver);
set n_altitueNumber=(select count(*)  from tb_estimate_detail 
where 1=1 and receiver=new.receiver);
set n_altitue=(select sum(altitue) from tb_estimate_detail where 1=1 and receiver=new.receiver);
#专业水平之和 除以总条数
case when n_number<0 
   then 
      set professiona=0;
   else 
      set professiona=n_professional /n_number;
 end case;
#服务态度数之和除以总条数
case when n_altitueNumber<0 
     then set altitu=0 ;
  else 
     set altitu=n_altitue / n_altitueNumber;
 end case;
#修改医生信息
update tb_doctor_baseinfo set feedbackrate=praise_number,professional=professiona,
attitude=altitu where 1=1 and aliasAccount=new.receiver;
end

你可能感兴趣的:(MySQL 下创建触发器)