Get current updated column name to use in a trigger

Just use OLD.colname <> NEW.colname for each column to check and find those that are different. Triggers are a bit limited in their use in MySQL, too bad.


Try this code...

create table sales (
  orderno INT, 
  sale INT,
  empsalary int,
  ts TIMESTAMP
);

create table history (
  updated varchar(20),
  oldvalue INT,
  newvalue INT
);

INSERT INTO sales 
  (orderno, sale, empsalary) 
VALUES
  (1,700,7000),
  (2,800,8000),
  (3,900,9000);

DROP TRIGGER test.instrigger;

DELIMITER ///

CREATE TRIGGER test.instrigger
AFTER UPDATE ON sales
FOR EACH ROW

BEGIN

    IF NEW.sale <> OLD.sale THEN  
        INSERT INTO history
          (updated, oldvalue, newvalue) 
        VALUES
          ('sale', OLD.sale,NEW.sale);
    END IF;
  
    IF NEW.empsalary <> OLD.empsalary THEN  
        INSERT INTO history
          (updated, oldvalue, newvalue)
        VALUES
          ('empsalary', OLD.empsalary,NEW.empsalary);
    END IF;
END;
///

DELIMITER ;