2017-12-20 9 views
-3

開始都市と終了都市に応じて1列にレンタカーの距離を置くトリガーを作成します。 これを試しましたが動作しませんMySQL:IFを使用してトリガーを作成する方法

`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;` 

これが間違っている場所を教えてください。

ありがとうございました

+2

CASE/WHEN構文を探します。 –

+1

マルチステートメントトリガーのBEGIN ... ENDを探します。 if-elseifは適切な構文を使用していれば動作します。 SETは更新されません。 @DanielEとして。 「NEW.km =ケース...いつ、いつ、いつ、...、そう、終わり、終わり」がうまくいくでしょう。 – Uueerdo

+0

は、だから、 のようなものは '各ROW セットnew.km =ケースのためにMI__Rent に挿入した後、トリガーKmDrivenを作成する意味、 StartCity = 1とEndCity = 2、その後New.km = 600、 を設定するとき[...] elseStartCity = 4 EndCity = 1 New.km = 300、 end; ' –

答えて

1

別のテーブルのトリガーを作成する必要があります。 mi_rentに挿入し、同じ行をトリガで更新しています。 このシナリオでは、MySqlでエラーが発生します。

ストアドファンクション/トリガーでテーブル 'mi_rent'を更新できません。このストアドファンクション/トリガーを呼び出したステートメントで既に使用されているためです。

+0

これで解決策は何でしょうか?どうすれば別のトリガーを作成できますか? –

関連する問題