DELIMITER $$
    DROP TRIGGER IF EXISTS ravi.`after_insert`$$
    CREATE
   /*[DEFINER = { user | CURRENT_USER }]*/
   TRIGGER `ravi`.`after_insert` AFTER UPDATE
  ON `ravi`.`user_data`
  FOR EACH ROW
  BEGIN`
 IF(NEW.field1 !=OLD.field1)THEN
 INSERT INTO logs_data(uri,field_id,old_value,new_value,modified_date)
 VALUES(NEW.uri,"filed1",OLD.field1,NEW.field1,NOW());
 END IF;
 IF(NEW.field2!=OLD.field2)THEN
 INSERT INTO logs_date(uri,field_id,old_value,new_value,modified_date)
 VALUES(NEW.uri,"field2",OLD.field2,NEW.field2,NOW());
 END IF;
 END$$

 DELIMITER ;


值未插入第二个表中,但触发器正在运行
   触发器不起作用

最佳答案

我相信您打算使用IF语句而不是IF()函数
触发器after_insert的名称令人困惑,因为您正在使用AFTER UPDATE事件
在第二个'filed1'语句中也有一些错别字'field1'代替log_date,更重要的是log_data代替INSERT


话虽如此,您的触发器应该看起来像

DELIMITER $$
CREATE TRIGGER user_data_after_update
AFTER UPDATE ON user_data
FOR EACH ROW
BEGIN
    IF NEW.field1 != OLD.field1 THEN
        INSERT INTO logs_data(uri, field_id, old_value, new_value, modified_date)
        VALUES(NEW.uri, 'field1', OLD.field1, NEW.field1, NOW());
    END IF;
    IF NEW.field2 != OLD.field2 THEN
        INSERT INTO logs_data(uri, field_id, old_value, new_value, modified_date)
        VALUES(NEW.uri, 'field2', OLD.field2, NEW.field2, NOW());
    END IF;
END$$
DELIMITER ;


这是SQLFiddle演示

09-28 00:06