我的表有这样的结构:
products:
mrp | discount | price
我想创建一个触发器,这样如果我更改折扣值,它就会相应地设置价格。
create trigger updateprice on products
after update
as if(update(discount))
update products set price=((100-discount)/100)*mrp;
但是这给了我语法错误&我无法找到它的确切代码。有人可以帮忙吗?
最佳答案
您需要一个 BEFORE
触发器,但不需要 AFTER
。
delimiter //
create trigger updateprice before update on products
for each row begin
if new.discount <> old.discount then
set new.price = ( ( 100 - new.discount ) / 100 ) * new.mrp;
end if;
end;
//
delimiter ;
关于mysql - 创建触发器以在更新第一列时更新第二列的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21612322/