Mysql触发器在更新时保留列子容量if语句

问题描述 投票:0回答:1

我想设置检查类容量,如果全班应该确认。当我更新保留时,它会给出错误。请帮忙。谢谢。

CREATE TRIGGER `control_class_capacity` 
AFTER UPDATE ON  `learningcenter_class`
FOR EACH ROW BEGIN

IF NEW.capacity - NEW.reserved = 0 THEN BEGIN

UPDATE learningcenter_class SET isconfirm = 1 WHERE class_id = NEW.class_id;

END; END IF;
END
mysql sql triggers
1个回答
0
投票

假设您的class_id是唯一的,您可以将触发器更改为before触发器并设置NEW.ISCONFIRM。顺便说一下,MYSQL中的语句不需要begin和end语句。

drop table if exists t;
create table t(class_id int ,capacity int, reserved int, isconfirm int);
insert into t values (1,1,0,0) , (2,1,0,0);

drop trigger if exists `control_class_capacity`;
delimiter $$
CREATE TRIGGER `control_class_capacity` 
before UPDATE ON  t
FOR EACH ROW BEGIN

IF NEW.capacity - NEW.reserved = 0 THEN

SET new.isconfirm = 1;

END IF;
END $$

delimiter ;

update t set reserved = 1 where class_id = 1;

select * from t;

+----------+----------+----------+-----------+
| class_id | capacity | reserved | isconfirm |
+----------+----------+----------+-----------+
|        1 |        1 |        1 |         1 |
|        2 |        1 |        0 |         0 |
+----------+----------+----------+-----------+
2 rows in set (0.00 sec)
© www.soinside.com 2019 - 2024. All rights reserved.