简体   繁体   English

编辑另一个mysql列时进行更新

[英]Update one mysql column when another is edited

I'm trying to make a datetime field that automatically gets updated with the current time only if there was a change to a certain field. 我正在尝试创建一个datetime字段,该字段仅在对某个字段进行更改的情况下才自动以当前时间进行更新。

It seems I have a syntax error. 看来我有语法错误。

CREATE OR ALTER TRIGGER last_progress_date
ON wp_task_mgr
AFTER UPDATE  
AS BEGIN
   IF UPDATE (progress_percentage)
   SET last_progress_date = GETDATE()
END

Just for future reference, I found the answer here: 仅供以后参考,我在这里找到了答案:

https://dba.stackexchange.com/questions/125203/simple-trigger-to-update-two-columns-in-one-table https://dba.stackexchange.com/questions/125203/simple-trigger-to-update-two-columns-in-one-table

MySQL query: MySQL查询:

DELIMITER //
CREATE TRIGGER trig_1 before insert
ON  <table_name> FOR EACH ROW
BEGIN

   IF   new.due_date is not null and new.end_date='' then
        set new.end_date=new.due_date;
   end if;
      IF   new.end_date is not null and new.due_date='' then
        set new.due_date=new.end_date;
   end if;

END;
//


DELIMITER //

CREATE TRIGGER trig_2 before update
ON  <table_name> FOR EACH ROW
BEGIN 

   IF   new.due_date <>old.due_date then
        set new.end_date=new.due_date;
   end if;
      IF   new.end_date <> old.end_date then
        set new.due_date=new.end_date;
   end if;

END;
//

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM