如何在触发器中添加条件?

时间:2014-10-27 02:01:56

标签: mysql

我有查询创建触发器:

CREATE TRIGGER `Insert` AFTER INSERT ON `HistoryBalance`
 FOR EACH ROW INSERT INTO Winners (name) VALUES  (new.ProductID)

但现在我想得到条件:

if (new.Status = 'user'){
   CREATE TRIGGER `Insert` AFTER INSERT ON `Balance`
 FOR EACH ROW INSERT INTO Winners (name) VALUES  (new.ProductID)
}

请告诉我克利特在我的情况下如何正确触发?

1 个答案:

答案 0 :(得分:0)

你可以这样做:

delimiter $$
CREATE TRIGGER balance_insert AFTER INSERT ON Balance
    FOR EACH ROW
BEGIN
    if (new.Status = 'user') then
        INSERT INTO Winners(name) VALUES  (new.ProductID);
    end if;
END; $$
delimiter ;

您可以阅读更多Here