MYSQL寫觸發器遇到的問題

2022-06-23 17:21:10 字數 919 閱讀 3427

今天有同學過來問我mysql的儲存過程怎麼寫,我按我的經驗寫好了,call一下,也正確。

於是他問了下上次沒解決的觸發器怎麼做?我就思考了一番:覺得可以先試試上次寫了1個半小時的方法。

--- 觸發器:實現插入開始時間和結束時間自動生成執行時間

delimiter ;;

create trigger `inserttraininfo` before insert on `traininfo` for each row begin

declare curtime datetime;

select new.trainendtime - new.trainstarttime into curtime from traininfo where trainno=new.trainno limit 1;

set new.trainrunningtime = curtime;

end;;

delimiter ;

drop trigger if exists `inserttraininfo`;

delimiter ;;

create trigger `inserttraininfo` before insert on `traininfo` for each row begin

set new.trainrunningtime = sec_to_time(unix_timestamp(new.trainendtime) - unix_timestamp(new.trainstarttime ));

end;;

delimiter ;

time = sec_to_time(unix_timestamp(time2)- unix_timestamp(time1));