在mysql触发器

时间:2016-04-07 16:03:51

标签: mysql database-trigger

我已经像这样写了我的触发器

CREATE TRIGGER `update_total_duration` AFTER UPDATE ON `sales_activity`
 FOR EACH ROW thisTrigger:BEGIN  
IF(NEW.activity_id=4) 
    THEN LEAVE thisTrigger;
ELSEIF NOT EXISTS
    (SELECT 1 FROM sales_duration_update WHERE user_id = NEW.user_id AND   date = CURDATE()) 
    THEN INSERT INTO sales_duration_update (user_id,date,total_duration) VALUES (NEW.user_id,CURDATE(),NEW.duration);
ELSE
    //problem is here when total_duration = total_duration + NEW.duration
    UPDATE sales_duration_update SET total_duration = total_duration + NEW.duration WHERE user_id = NEW.user_id AND date = CURDATE();
END IF;

END

问题在于代码 total_duration = total_duration + NEW.duration 其中两个变量都是timestamp变量。如何在触发器中添加两个timestamp变量?

1 个答案:

答案 0 :(得分:-1)

我找到了答案。可能会帮助别人。

CREATE TRIGGER `update_total_duration` AFTER UPDATE ON `sales_activity`
FOR EACH ROW thisTrigger:BEGIN  
IF(NEW.activity_id=4) 
THEN LEAVE thisTrigger;
ELSEIF NOT EXISTS
(SELECT 1 FROM sales_duration_update WHERE user_id = NEW.user_id AND   date     = CURDATE()) 
    THEN 
INSERT INTO sales_duration_update (user_id,date,total_duration) VALUES     (NEW.user_id,CURDATE(),NEW.duration);
ELSE
UPDATE sales_duration_update SET total_duration = ADDTIME(total_duration,     NEW.duration) WHERE user_id = NEW.user_id AND date = CURDATE();
END IF;