mysql 触发器 update if,如何在mysql触发器中退出一系列If / else条件?

I have a trigger that checks to see if certain fields changed during the update.

If any of these fields changed I update another table.

I'd like to "break" out of the if conditions as soon as I know that something changed.

Is there a way to do this within a MySQL Trigger?

What I have works, but It seems inefficient.

CREATE TRIGGER profile_trigger

BEFORE UPDATE ON profile

FOR EACH ROW

BEGIN

DECLARE changed INTEGER;

SET changed = 0;

IF STRCMP(NEW.first_name, OLD.first_name) <> 0 THEN

SET changed = 1;

ELSEIF STRCMP(NEW.last_name, OLD.last_name) <> 0 THEN

SET changed = 1;

ELSEIF STRCMP(NEW.maiden_name, OLD.maiden_name) <> 0 THEN

SET changed = 1;

ELSEIF STRCMP(NEW.suffix, OLD.suffix) <> 0 THEN

SET changed = 1;

ELSEIF STRCMP(NEW.title, OLD.title) <> 0 THEN

SET changed = 1;

ELSEIF STRCMP(NEW.gender, OLD.gender) <> 0 THEN

SET changed = 1;

ELSEIF STRCMP(NEW.street, OLD.street) <> 0 THEN

SET changed = 1;

ELSEIF STRCMP(NEW.street2, OLD.street2) <> 0 THEN

SET changed = 1;

ELSEIF STRCMP(NEW.city, OLD.city) <> 0 THEN

SET changed = 1;

ELSEIF STRCMP(NEW.state, OLD.state) <> 0 THEN

SET changed = 1;

ELSEIF STRCMP(NEW.zip, OLD.zip) <> 0 THEN

SET changed = 1;

ELSEIF STRCMP(NEW.home_phone, OLD.home_phone) <> 0 THEN

SET changed = 1;

ELSEIF NEW.date_of_birth <> OLD.date_of_birth THEN

SET changed = 1;

END IF;

IF changed > 0 THEN

update other_table set updated = CURRENT_TIMESTAMP where id = NEW.id;

END IF;

END;

|

解决方案

That's how ELSEIF works inherently. Once MySQL finds any condition that passes (i.e., evaluates to TRUE) it will not evaluate any further conditions in the IF..ELSEIF..ELSE chain.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值