mysql触发器不工作

dwthyt8l  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(319)

我有三张table

tbl_payments(pay_id,date,amount,description,-------)
tbl_pay_trans(pay_id,trans_id)
tbl_transactions(trans_id,trans_date,trans_amount,trans_description,-----)

“tbl\u transaction”与“tbl\u payments”中的数据相同,还有一些其他值。为了保持两个表之间的关系,我使用了“tbl\u pay\u trans”。我想做的是,当更新tbl\u付款(金额,描述)时,需要在tbl\u交易(trans\u金额,trans\u描述)中做相同的更改。我编写了一个触发器来实现这一点,但它并没有像预期的那样更新tbl\u事务表的值。
我的扳机是

DELIMITER $$
CREATE TRIGGER update_trans 
    AFTER UPDATE on tbl_payments
    FOR EACH ROW
BEGIN
    DECLARE new_amount VARCHAR(50);
    DECLARE new_description TEXT;
    DECLARE new_pay_id,new_trans_id INT;

    SET new_pay_id = OLD.pay_id;
    SET new_amount = OLD.amount;
    SET new_description = OLD.description;
    SELECT trans_id INTO new_trans_id FROM tbl_pay_trans WHERE pay_id = new_pay_id;

    UPDATE tbl_transactions SET 
    trans_amount = new_amount,
    trans_description = new_description
    WHERE trans_id = new_trans_id;
END$$
DELIMITER ;

请有人帮我找出我做错了什么。

pgccezyw

pgccezyw1#

它没有更新,因为您正在使用 OLDamount 以及 description 需要使用的列 NEW

SET new_amount = NEW.amount;
SET new_description = NEW.description;
``` `OLD` 指中发生更新之前的列值 `tbl_payments` . 请参阅手册。

相关问题