如何修复DATEDIFF触发器中的语法错误

时间:2019-03-28 13:35:28

标签: mysql

任何人都可以看到此代码有什么问题吗?

一切正常,直到我添加触发器来计算两个日期之间的差。首先,我改变了桌子,以防万一有人想知道。

ALTER TABLE bookings 
ADD COLUMN TheDuration varchar(10);  

然后我的触发器如下...

DELIMITER //

CREATE TRIGGER check_licence /*This trigger will approve customers with a valid licence */
BEFORE UPDATE ON customers                 
FOR EACH ROW 
BEGIN 
    SET NEW.Status = CASE WHEN NEW.valid_licence = 'Yes' 
                          THEN 'Approved'
                          ELSE 'Unapproved'  /*So if a Customer has a valid licence, He will be automatically approved. */
                                        /*But if he doesn't he will become unapproved[WORKING]*/

SET NEW.TheDuration = DATEDIFF(NEW.bookings.end_date, NEW.bookings.start_date)   -- -- TO CALCULATE DURATION BETWEEN 2 DATES
END;
//  
DELIMITER ;

问题出在

SET NEW.TheDuration = DATEDIFF(NEW.bookings.end_date, NEW.bookings.start_date)   -- -- TO CALCULATE DURATION BETWEEN 2 DATES

在我添加所有内容之前,一切正常。

在这里...

DELIMITER //
CREATE TRIGGER Carperperson  /* This Trigger Blocks a customer from renting two cars on the same name twice on one day. */
BEFORE INSERT ON bookings   /*E.g. Mr.ABC cannot rent a Ford and a Nissan on the same day. Has to return first car first.[WORKING]*/
FOR EACH ROW 
BEGIN
IF EXISTS (
    SELECT 1
    FROM bookings 
    WHERE NEW.customer_id = bookings.customer_id
      AND ((new.start_date >= bookings.start_date 
            and new.start_date < bookings.end_date)
        or (new.end_date > bookings.start_date 
            and new.end_date < bookings.end_date))
) THEN
    SIGNAL SQLSTATE '45000' 
        set message_text='You can only book one car per single customer a day!' ; /* This triggers only allows to rent a car for 7 days, not more, not less[WORKING]*/
END IF;
        IF ( NEW.end_date > NEW.start_date + INTERVAL 7 DAY ) THEN
   SIGNAL SQLSTATE '45000'
   SET MESSAGE_TEXT = '7 is the maximum. Please choose an earlier date.'; /*The end_date is more than seven days after start_date*/

   END IF;   
 SET NEW.TheDuration = DATEDIFF(NEW.bookings.end_date, NEW.bookings.start_date)  -- -- TO CALCULATE DURATION BETWEEN 2 DATES
END;
//

DELIMITER ;

问题在于...

SET NEW.TheDuration = DATEDIFF(NEW.bookings.end_date, NEW.bookings.start_date)  -- -- TO CALCULATE DURATION BETWEEN 2 DATES

0 个答案:

没有答案