如何使用Mysql触发'leave','switch'命令

时间:2012-05-15 03:01:36

标签: mysql triggers

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

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'被击中,它仍将通过所有其他ifs。

  1. 有更好的方法吗?
  2. 在'结束之前'使用'离开',如果'一个好主意
  3. 由于

2 个答案:

答案 0 :(得分:0)

你试过这个吗?

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;

答案 1 :(得分:0)

试试这个。

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;