有一个名为'Coach'的MySQL表,它具有以下属性。
coach_ID int
mileage_of_last_service_date double
mileage double
isAvailable boolean
对于所有行,isAvailable最初设置为1。我想设置一个触发器来执行以下操作。 当里程属性发生更新并且里程数大于100000时,触发器必须为同一个表中的特定属性设置属性,如下所示。
isAvailable = 0
mileage_of_last_service_date = mileage_of_last_service_date + mileage
mileage = 0
这是我写过的查询。
create trigger set_availability_coach after update on coach
for each row
when old.mileage > 10000
begin
update coach
set isAvailable = 0, mileage_of_last_service_date = mileage_of_last_service_date + old.mileage, mileage = 0
where coach.coach_ID = old.coach_ID;
end;
当我运行这个时,我在第3行遇到语法错误。 有没有解决这个问题? 或者任何其他方式来实现此功能?
答案 0 :(得分:1)
我改编了这个,这应该没问题
CREATE TRIGGER set_availability_coach BEFORE UPDATE ON coach
FOR EACH ROW BEGIN
IF OLD.mileage > 10000 THEN
SET NEW.isAvailable = 0,
NEW.mileage_of_last_service_date = OLD.mileage_of_last_service_date + NEW.mileage,
NEW.mileage = 0;
END IF;
END
答案 1 :(得分:0)
您只能在OLD
- 触发器see MySQL docs访问BEFORE
。
如果您不需要UPDATE ... SET ...
查询,则可以创建BEFORE UPDATE
触发器,并在适当的位置修改NEW
字段。