触发插入触发器后的冗余条目

时间:2017-11-13 16:19:39

标签: mysql sql triggers

我有一个用户界面,将分期付款作为输入,将其保存到分期付款表(t_received_money_item)中。每个分期付款都与其订单号相关联。订单号是主要ID。

在分期付款表上,我有一个名为trg_t_received_money_item_insert_setReceivedMoneyUpdate

的触发器

这是一个AFTER INSERT触发器。触发器将数据插入名为t_credit_system_money的表中。但是,我的触发器问题是: 1.在表中插入多个分期后,冗余条目将插入t_credit_system_money

例如:

如果我将值插入分期付款表(t_received_money_item) 为bill_customer_code = 1的1000金额 然后,t_credit_system_money将为bill_customer_code = 1提供1000金额的条目。

如果我将两条记录插入t_received_money_item作为1000金额的bill_customer_code = 1和2000金额的bill_customer_code = 2,那么 对于bill_customer_code = 1,t_credit_system_money将包含1000金额的条目,而对于bill_customer_code = 1,t_credit_system_money将具有1000金额的条目,而t_credit_system_money将为bill_customer_code = 2具有2000金额的条目。

t_credit_system_money

中插入3次后,t_received_money_item中的6条记录同样如此
DELIMITER $$

USE `kikan_db`$$

DROP TRIGGER /*!50032 IF EXISTS */ `trg_t_received_money_item_insert_setReceivedMoneyUpdate`$$

CREATE
   /*!50017 DEFINER = 'kikan'@'%' */
    TRIGGER `trg_t_received_money_item_insert_setReceivedMoneyUpdate` AFTER INSERT ON `t_received_money_item` 
FOR EACH ROW BEGIN

DECLARE new_order_no VARCHAR(15);

  SELECT order_no INTO new_order_no FROM t_received_money  WHERE `t_received_money`.bill_customer_code = new.bill_customer_code;  


INSERT INTO t_credit_system_money(
                                data_created,
                                process_flag,
                                data_flag,
                                bill_customer_code,
                                bill_spot_customer_code,
                                order_no,
                                amount,
                                amount2,
                                credit_update_flag,
                                delete_flag,
                                create_user,
                                update_user,
                                updated,
                                deleted,
                                created
                            )
                            VALUES
                            (
                                NOW(),
                                1,
                                1,
                                NEW.bill_customer_code,
                                IFNULL(NEW.bill_spot_customer_code,0),
                                new_order_no,
                                NEW.received_money,
                                0,
                                0,
                                0,
                                NEW.update_user,
                                NEW.update_user,
                                NEW.updated,
                                NULL,
                                NEW.updated
                            );

END;
$$

DELIMITER ;

0 个答案:

没有答案