如果那么Mysql触发错误

时间:2017-11-22 23:42:20

标签: mysql sql innodb

我遇到触发器问题..如果新的returnBike不为null,我想将值更改为false。

    CREATE TRIGGER returnBikeTrigger
AFTER UPDATE ON Rent
FOR EACH ROW
    IF (NEW.returnBike <> null) THEN
        UPDATE Bike SET isRented = false
        WHERE id = (select Bike from Rent); -- but there is Error (missing semicolon)
        END IF;

有什么建议吗?

2 个答案:

答案 0 :(得分:0)

DELIMITER $$    
CREATE TRIGGER returnBikeTrigger
AFTER UPDATE ON Rent
FOR EACH ROW
BEGIN
    IF (NEW.returnBike IS NOT NULL) THEN
        UPDATE BikeSET isRented = false
        WHERE id = new.Bike;
    END IF;
END; $$
DELIMITER ; 

所以要删除语法分号的问题,在结束后添加分隔符和$$; 无论如何,谢谢你给我一些建议if if comparison!

答案 1 :(得分:0)

我倾向于这样做:

DELIMITER $$    
CREATE TRIGGER returnBikeTrigger
AFTER UPDATE ON Rent
FOR EACH ROW
BEGIN
    UPDATE Bike
        SET isRented = false
    WHERE id = new.Bike AND NEW.returnBike IS NOT NULL;
END; $$
DELIMITER ;