MySQL Trigger在插入之前修改col

时间:2016-10-31 04:42:32

标签: mysql sql database

我想在插入记录之前修改col!我有交易表和我希望设置0.5%佣金的每笔新交易。

问题是:他一直在0 col中插入amount。但我想要当前的current value x 0.50%

DROP TRIGGER IF EXISTS `viptest`.`testing`;
DELIMITER $$

CREATE
    /*[DEFINER = { user | CURRENT_USER }]*/
    TRIGGER `viptest`.`commission_before_insert` BEFORE INSERT
    ON `viptest`.`transaction_history`
    FOR EACH ROW        
    BEGIN
       SET NEW.amount = NEW.amount % 0.50';
    END$$

DELIMITER ;

1 个答案:

答案 0 :(得分:0)

试试这个

DROP TRIGGER IF EXISTS `viptest`.`testing`;
DELIMITER $$

CREATE
/*[DEFINER = { user | CURRENT_USER }]*/
TRIGGER `viptest`.`commission_before_insert` BEFORE INSERT
ON `viptest`.`transaction_history`
FOR EACH ROW        
BEGIN

   -- If you just want to modify current row, this line should work
   SET NEW.amount = NEW.amount * 0.5 / 100;

   -- If you want to process this commission into another table, you can use this line
   insert into db_name.table_name (col1, col2, col3)
   select (val1, NEW.amount * 0.5 / 100, val3);

END$$

DELIMITER ;