在 MySQL 中使用触发器监视用户资产变更

创建:

DELIMITER ^^
CREATE TRIGGER t_user_asset_watch
	BEFORE UPDATE ON users
	FOR EACH ROW 
BEGIN
        IF (OLD.balance!=NEW.balance or OLD.balance_usdt!=NEW.balance_usdt 
            or OLD.frozen!=NEW.frozen or OLD.frozen_usdt!=NEW.frozen_usdt) THEN
            INSERT INTO user_asset_watch(user_id,balance_old,balance_new,frozen_old,frozen_new) 
            VALUES(OLD.id,OLD.balance,NEW.balance,OLD.frozen,NEW.frozen);
        END IF^^
END^^
DELIMITER ;

删除:

DROP TRIGGER IF EXISTS t_user_asset_watch;