1

我有触发器,如下例所示:

BEFORE UPDATE ON icharts_user_stats FOR EACH ROW BEGIN

IF (NEW.col1> OLD.col1
    OR NEW.col2 > OLD.col2) THEN 
    SET NEW.col3 =  NEW.col3+1;
    SET NEW.col4 =  NEW.col4+1; END  IF;

IF (NEW.col5> OLD.col5
    OR NEW.col6 > OLD.col6) THEN 
    SET NEW.col7 =  NEW.col7+1; END  IF;

IF (NEW.col8> OLD.col8
    OR NEW.col9 > OLD.col9) THEN 
    SET NEW.col10 =  NEW.col10+1; END  IF;

这类似于 switch 语句,在任何时候都只会执行一个“if”。上面的代码有效,但如果第一个“if”被命中,它仍然会通过所有其他的 if。

  1. 有更好的方法吗?
  2. 在“结束如果”之前使用“离开”是个好主意

谢谢

4

2 回答 2

3

尝试这个。

BEFORE UPDATE ON icharts_user_stats FOR EACH ROW 
icharts_user_stats:BEGIN

IF (NEW.col1> OLD.col1
    OR NEW.col2 > OLD.col2) THEN 
    SET NEW.col3 =  NEW.col3+1;
    SET NEW.col4 =  NEW.col4+1; 
    leave icharts_user_stats;
END  IF;

IF (NEW.col5> OLD.col5
    OR NEW.col6 > OLD.col6) THEN 
    SET NEW.col7 =  NEW.col7+1; 
    leave icharts_user_stats;
END  IF;

IF (NEW.col8> OLD.col8
    OR NEW.col9 > OLD.col9) THEN 
    SET NEW.col10 =  NEW.col10+1; 
    leave icharts_user_stats;
END  IF;
于 2017-03-17T12:54:11.263 回答
-1

你试过这个吗?

BEFORE UPDATE ON icharts_user_stats FOR EACH ROW BEGIN

    IF (NEW.col1> OLD.col1
        OR NEW.col2 > OLD.col2) THEN 
        SET NEW.col3 =  NEW.col3+1;
        SET NEW.col4 =  NEW.col4+1; 

    ELSEIF (NEW.col5> OLD.col5
        OR NEW.col6 > OLD.col6) THEN 
        SET NEW.col7 =  NEW.col7+1; 

    ELSEIF (NEW.col8> OLD.col8
        OR NEW.col9 > OLD.col9) THEN 
        SET NEW.col10 =  NEW.col10+1; END  IF;
于 2012-05-15T03:11:36.023 回答