我有表lbs_records,字段:
taken TIMESTAMP NOT NULL DEFAULT current_timestamp(),
expected TIMESTAMP NULL DEFAULT NULL ,
我有触发器:
DELIMITER $$
DROP TRIGGER IF EXISTS trig_records_correct $$
CREATE TRIGGER trig_records_correct BEFORE INSERT ON lbs_records
FOR EACH ROW BEGIN
IF ( NEW.expected IS NULL ) THEN
SET NEW.expected = adddate( NEW.taken, ( SELECT lbs_books.`4home_ondays`
FROM lbs_books WHERE lbs_books.id = NEW.book_id ) );
END IF;
UPDATE lbs_books
SET lbs_books.in_stock = lbs_books.in_stock -1
WHERE lbs_books.id = NEW.book_id;
END $$
DELIMITER ;
当我尝试在此表中插入行时,我的触发器仅在lbs_books.in_stock中为id = NEW.book_id的行更改(减少)值。但是现在插入的行不会改变“预期”字段。在插入行之后,'expected'字段为空。
- 我是对的,当触发器开始为查询
INSERT INTO lbs_records (user_id, book_id) VALUES (5,1);
工作时,它也会获得带字段的行 'take':'curr datetime','expected':'null'?
- 为什么在插入行之后,这行包含即使在触发器工作后为null的字段'expected'?
醇>
答案 0 :(得分:0)
在插入触发器之前使用MySQL时,未设置列的默认值,因此要么更改选择以再次添加默认值...
.SD
或者手动获取值...
SET NEW.expected = adddate( CURRENT_TIMESTAMP(), ( SELECT lbs_books.`4home_ondays`
FROM lbs_books WHERE lbs_books.id = NEW.book_id ) );
或者更长的路......
SET NEW.taken = DEFAULT(lbs_books.taken)