在我确认数字符合预期之后,以MYSQL
为条件的以下trigger
总是对ELSE
语句求值。如果我做出以下假设:NEW.exit_time = '2019-04-09 11:50:00'
,OLD.enter_time = '2019-04-09 11:00:00'
我期望time_in_minutes = 50
,如果SET NEW.total = 25
,则应该plot_minimum_cost = 25
。我已经验证了填充plot_minimum_cost和plot_minute_cost的select语句是正确的,因为plot_max_cost运行正常并且使用相同的逻辑进行检索。我想念什么?
DROP TRIGGER IF EXISTS get_total_parking_cost;
DELIMITER $$
CREATE TRIGGER get_total_parking_cost BEFORE UPDATE
ON records
FOR EACH ROW
BEGIN
DECLARE plot_minimum_cost DOUBLE;
DECLARE plot_max_cost DOUBLE;
DECLARE plot_minute_cost DOUBLE;
DECLARE time_in_lot DATETIME;
DECLARE time_in_minutes DOUBLE;
SELECT parking_lots.min_cost INTO plot_minimum_cost
FROM parking_lots
WHERE parking_lots.id = OLD.parking_id;
SELECT parking_lots.max_cost INTO plot_max_cost
FROM parking_lots
WHERE parking_lots.id = OLD.parking_id;
SELECT parking_lots.minutely_cost INTO plot_minute_cost
FROM parking_lots
WHERE parking_lots.id = OLD.parking_id;
SET @time_in_lot = TIMEDIFF(NEW.exit_time, OLD.enter_time);
SET @time_in_minutes = (HOUR(time_in_lot) * 60) + MINUTE(time_in_lot);
IF @time_in_minutes <= 60 THEN
SET NEW.total = plot_minimum_cost;
ELSEIF @time_in_minutes <= 300 THEN
SET NEW.total = time_in_minutes * plot_minute_cost;
ELSE
SET NEW.total = plot_max_cost;
END IF;
END$$
DELIMITER ;
答案 0 :(得分:0)
您已经声明了局部变量time_in_lot
,并在(HOUR(time_in_lot) * 60) + MINUTE(time_in_lot)
中使用了它;但您从未真正设置过它。您设置@time_in_lot
,这是一个会话变量。同样,即使您已声明@time_in_minutes
,也要设置time_in_minutes
;但是在这种情况下,您将使用@
版本的IF条件。
基本上,从触发器中删除@
字符的所有实例,看看是否可以解决。