如何在mysql中用多个条件编写触发器

时间:2016-08-03 13:45:11

标签: mysql database-trigger

我制作了两张包含以下字段的表格:

TABLE 1: "demo"
FIELD: "demoid"
FIELD: "cpid"
FIELD: "status"     


TABLE 2: "sample"
FIELD: "id"
FIELD: "conid"    
FIELD: "statussample"
FIELD: "date"

每当示例表字段status发生更改时,我想更新statussample。 条件是

1) `demoid` and `id` would be same and `cpid` and `conid` would be same.(if more than one values comes i want the last inserted value of  `sample` using the field `date`)

如何做到这一点?我坚持使用这个......更新为触发格式

1 个答案:

答案 0 :(得分:1)

DELIMITER //

CREATE TRIGGER sample_after_update
AFTER UPDATE
    ON sample FOR EACH ROW

BEGIN

   UPDATE demo
   SET demoid = NEW.id,
       cpid   = NEW.conid

END; //
DELIMITER;