mysql insert 触发器
添加insert触发器,在insert一条新纪录时,当主单号不为空,并且新增价格和最近一次价格对比不相等时,说明价格有变化。这时触发器会自动将上一次老价格添加到当前新增行的unit_price_old老价格列。
这个需求是在一个表上,更新自己身上的其他列,这时需要用before,并且set new.列名,并且new必须在等号左边。
delimiter //
create trigger insert_flight_cabin_unit_price
before
insert on flight_cabin_book_o_update
for each row
begin
/*注意給变量赋值,等号右边的select必须加括号*/
/*获取每条主单号的最近一次更新时间*/
set @last_creat_time = (select creat from flight_cabin_book_o_update where waybill=new.waybill and flight_no=new.flight_no and flight_time=new.flight_time order by creat desc limit 1);
/*获取每条主单号的最近一次更新价格*/
set @last_unit_price = (select unit_price from flight_cabin_book_o_update where waybill=new.waybill and flight_no=new.flight_no and flight_time=new.flight_time and creat = @last_creat_time limit 1);
/*如果一个主单号不为空,并且最近一次价格和现在insert的价格不相同,就说明价格更新了*/
if new.waybill is not null and new.waybill !='' and new.unit_price != @last_unit_price then
set new.unit_price_old = @last_unit_price;
set new.unit_price_old_time = @last_creat_time;
end if;
end //
delimiter ;
mysql update触发器
这个是在一个表上update自己身上的其他列,也需要用before,并且
set new.列名,并且new必须在等号左边
delimiter //
create trigger update_flight_cabin_unit_pric