简体   繁体   English

带触发器的MySQL列更新

[英]MySQL Column Update with trigger

I have this reservation table that has RESERVATION_ID , ROOM_NUM, Date_Start , Date_End and cost columns . 我的预订表具有RESERVATION_ID , ROOM_NUM, Date_Start , Date_End and cost columns what I want to do is insert all the columns except cost and fill the cost automatically. 我想要做的是插入除成本以外的所有列,并自动填充成本。

delimiter //
CREATE TRIGGER upd_check before INSERT ON reservation
    FOR EACH ROW
    BEGIN
        IF NEW.RESERVATION_COST = NULL THEN
            SET NEW.RESERVATION_COST = 'timestampdiff(day, NEW.RESERVATION_STARTD, NEW.RESERVATION_ENDD)*70';
        END IF;
    END;//
delimiter ;

I wrote this trigger to do it, but whenever I press Apply to insert everything nothing is inserted to the RESERVATION_COST column. 我编写了此trigger来做到这一点,但是每当我按Apply(应用)插入所有内容时,什么都不会插入RESERVATION_COST列。

why? 为什么?

I would put this in a comment if I had enough reputation, but anyways. 如果我有足够的声誉,我会对此发表评论,但是无论如何。 Triggers cannot act on the same table which activated them. 触发器不能作用于激活它们的同一表。 Seems limiting, but we've had the same issue. 似乎有限制,但我们遇到了同样的问题。

This link doesn't explicitly say this but it does say "in the associated table". 该链接没有明确说明这一点,但确实说“在关联表中”。 Not the same: https://dev.mysql.com/doc/refman/5.0/en/triggers.html 不一样: https//dev.mysql.com/doc/refman/5.0/en/triggers.html

You can't compare a value to NULL , you need to check if it IS NULL instead. 您不能将值与NULL比较,而需要检查它是否为IS NULL

eg 例如

 IF NEW.RESERVATION_COST IS NULL THEN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM