mysql Trigger for logging, find changed columns

后端 未结 2 1741
庸人自扰
庸人自扰 2020-12-31 19:51

I am writing a trigger to keep track of all the changes that happens in a table. Unfortunately the table has 150+ columns and I wanted to avoid writing each column in the c

2条回答
  •  清酒与你
    2020-12-31 20:40

    As ingratiatednerd already suggested, you can use CONCAT_WS to make strings out of all required values and make a single compare statement.

    Perhaps the following is useful to someone:

    DECLARE old_concat, new_concat text;
    SET old_concat = CONCAT_WS(',', OLD.fld1, OLD.fld2, ...);
    SET new_concat = CONCAT_WS(',', NEW.fld1, NEW.fld2, ...); 
    
    IF old_concat <> new_concat
    THEN
       INSERT STATEMENT
    END IF;
    

提交回复
热议问题