MYSQL 条件总是评估为 Else
MYSQL Conditional Always Evaluating to Else
trigger
中的以下 MYSQL
条件总是在我验证数字符合预期后评估 ELSE
语句。如果我做出以下假设:NEW.exit_time = '2019-04-09 11:50:00'
、OLD.enter_time = '2019-04-09 11:00:00'
我期望 time_in_minutes = 50
,如果 plot_minimum_cost = 25
,则应该 SET NEW.total = 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 ;
您已声明局部变量 time_in_lot
并在 (HOUR(time_in_lot) * 60) + MINUTE(time_in_lot)
中使用它;但你从来没有真正设置它。您设置 @time_in_lot
这是一个会话变量。类似地,即使您声明了 time_in_minutes
,您仍在设置 @time_in_minutes
;但在那种情况下,您使用的是 @
版本的 IF 条件。
基本上,从触发器中删除 @
字符的所有实例,看看是否可以解决问题。
trigger
中的以下 MYSQL
条件总是在我验证数字符合预期后评估 ELSE
语句。如果我做出以下假设:NEW.exit_time = '2019-04-09 11:50:00'
、OLD.enter_time = '2019-04-09 11:00:00'
我期望 time_in_minutes = 50
,如果 plot_minimum_cost = 25
,则应该 SET NEW.total = 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 ;
您已声明局部变量 time_in_lot
并在 (HOUR(time_in_lot) * 60) + MINUTE(time_in_lot)
中使用它;但你从来没有真正设置它。您设置 @time_in_lot
这是一个会话变量。类似地,即使您声明了 time_in_minutes
,您仍在设置 @time_in_minutes
;但在那种情况下,您使用的是 @
版本的 IF 条件。
基本上,从触发器中删除 @
字符的所有实例,看看是否可以解决问题。