MySQL:如何使用IF创建触发器

问题描述 投票:-3回答:1

我想在mysql中创建一个触发器,根据开始的城市和最终城市,我将汽车租赁的距离放在一行,但是它不起作用

`create trigger KmDriven after insert on MI__Rent
for each ROW
set new.km = new.StartCity*new.EndCity,
if StartCity=1 and EndCity=2 then update New.km=600,
elseif StartCity=1 and EndCity=3 then update New.km=400,
elseif StartCity=1 and EndCity=4 then update New.km=800,
elseif StartCity=1 and EndCity=5 then update New.km=675,
elseif StartCity=2 and EndCity=1 then update New.km=600,
elseif StartCity=2 and EndCity=3 then update New.km=550,
elseif StartCity=2 and EndCity=4 then update New.km=300,
elseif StartCity=3 and EndCity=1 then update New.km=400,
elseif StartCity=3 and EndCity=2 then update New.km=550,
elseif StartCity=3 and EndCity=4 then update New.km=500,
elseif StartCity=3 and EndCity=5 then update New.km=300,
elseif StartCity=4 and EndCity=1 then update New.km=800,
elseif StartCity=4 and EndCity=2 then update New.km=300,
elseif StartCity=4 and EndCity=3 then update New.km=500,
elseif StartCity=4 and EndCity=5 then update New.km=300,
elseif StartCity=5 and EndCity=1 then update New.km=675,
elseif StartCity=4 and EndCity=1 then update New.km=500,
elseif StartCity=4 and EndCity=1 then update New.km=300,
elseif StartCity=4 and EndCity=1 then update New.km=300,
end if,
end;`

你能告诉我这是错的吗?

谢谢

mysql
1个回答
1
投票

您需要为另一个表创建触发器。您正在插入mi_rent并使用触发器更新相同的行。 MySql在这种情况下给出错误。

无法更新存储函数/触发器中的表'mi_rent',因为它已被调用此存储函数/触发器的语句使用。

© www.soinside.com 2019 - 2024. All rights reserved.