我有以下触发器,其中不执行CASE语句:
DELIMITER //
CREATE TRIGGER tri_result AFTER INSERT ON exp_result
FOR EACH ROW BEGIN
DECLARE priceMinWeekDay FLOAT DEFAULT 0;
DECLARE priceMinWeekEnd FLOAT DEFAULT 0;
DECLARE dayOfWeek INT;
DECLARE limitTop FLOAT;
DECLARE limitBottom FLOAT;
DECLARE proccessed INT DEFAULT 0;
-- Selects the minimum price that appears for this hotel during the last week days (not Saturdays and Sundays) in the last 7 days
SET priceMinWeekDay = (SELECT MIN(res_price) FROM exp_result JOIN exp_query ON que_id = res_que_id WHERE res_date BETWEEN NOW() - INTERVAL 7 DAY AND NOW() AND res_idHotel = NEW.res_idHotel AND que_day <> 6 AND que_day <> 7);
-- Selects the minimum price that appears for this hotel during the last Saturdays and Sundays in the last 30 days
SET priceMinWeekEnd = (SELECT MIN(res_price) FROM exp_result JOIN exp_query ON que_id = res_que_id WHERE res_date BETWEEN NOW() - INTERVAL 30 DAY AND NOW() AND res_idHotel = NEW.res_idHotel AND que_day = 6 OR que_day = 7);
-- Selects the day of week that has been currently scraped (6 = Saturday, 7 = Sunday)
SET dayOfWeek = (SELECT que_day FROM exp_query JOIN exp_result ON res_que_id = que_id WHERE res_id = NEW.res_id);
-- The maximum time a price can be smaller without raising an error
SET limitBottom = 0.7;
-- The maximum time a price can be higher without raising an error
SET limitTop = 1.7;
-- Set to 0 if
SET proccessed = 0;
-- Price can't be equal or smaller than 0 and bigger than 84000
IF proccessed = 0 THEN
IF NEW.res_price <= 0 THEN
INSERT INTO exp_alert (ale_res_id, ale_proccessed) VALUES (NEW.res_id, 0);
SET proccessed = 1;
END IF;
END IF;
IF proccessed = 0 THEN
IF NEW.res_price >= 84000 THEN
INSERT INTO exp_alert (ale_res_id, ale_proccessed) VALUES (NEW.res_id, 0);
SET proccessed = 1;
END IF;
END IF;
-- Case to compare week days and weekends between themselves
CASE
WHEN dayOfWeek = '6' THEN -- If Saturday
BEGIN
IF proccessed = 0 THEN
IF (NEW.res_price > limitTop*priceMinWeekEnd) THEN
INSERT INTO exp_alert (ale_res_id, ale_proccessed) VALUES (NEW.res_id, 0);
SET proccessed = 1;
END IF;
IF (NEW.res_price < limitBottom*priceMinWeekEnd) THEN
INSERT INTO exp_alert (ale_res_id, ale_proccessed) VALUES (NEW.res_id, 0);
SET proccessed = 1;
END IF;
END IF;
END;
WHEN dayOfWeek = '7' THEN -- If Sunday
BEGIN
IF proccessed = 0 THEN
IF (NEW.res_price > limitTop*priceMinWeekEnd) THEN
INSERT INTO exp_alert (ale_res_id, ale_proccessed) VALUES (NEW.res_id, 0);
SET proccessed = 1;
END IF;
IF (NEW.res_price < limitBottom*priceMinWeekEnd) THEN
INSERT INTO exp_alert (ale_res_id, ale_proccessed) VALUES (NEW.res_id, 0);
SET proccessed = 1;
END IF;
END IF;
END;
ELSE -- If weekday (not Saturday and Sunday)
BEGIN
IF proccessed = 0 THEN
IF (NEW.res_price > limitTop*priceMinWeekDay) THEN
INSERT INTO exp_alert (ale_res_id, ale_proccessed) VALUES (NEW.res_id, 0);
SET proccessed = 1;
END IF;
IF (NEW.res_price < limitBottom*priceMinWeekDay) THEN
INSERT INTO exp_alert (ale_res_id, ale_proccessed) VALUES (NEW.res_id, 0);
SET proccessed = 1;
END IF;
END IF;
END;
END CASE;
END;
表结构是:
表exp_hotel:
hot_id | hot_webid | hot_name |hot_starrating | hot_brandbool
7733871| 475 | Richwood Garden Hotel|0 | 0
表exp_query:
que_id | que_city | que_children | que_adults | que_week | que_day | que_staylength
13 | London | 0 | 2 | 2 | 6 | 1
表exp_result:
res_id | res_idHotel | res_rank | res_price | res_userRating | res_oldPrice | res_posa | res_date | res_que_id
33526 |7733871 |6 |234 |-1 |587 |us.website|2014-03-29 02:30:00|1
表exp_alert:
ale_id | ale_res_id | ale_proccessed
(Auto Inc.)
现在如果我在exp_result中插入这一行:
res_id | res_idHotel | res_rank | res_price | res_userRating | res_oldPrice | res_posa | res_date | res_que_id
(Auto Inc.)|7733871 |1 |100 |-1 |587 | us.website|2014-04-24 02:30:00|13
触发器完全适用于NEW.res_price为&lt; 0或&gt; 84000当我将res_price设置为0或84000时,但由于某些原因,当我提出较低的价格(例如100)时,它拒绝执行所有CASE语句中的代码。
我已将我在测试表中使用的所有变量存储起来并且所有值都正确(请注意,priceMinWeekDay或priceMinWeekEnd为NULL,这是正常的)。
答案 0 :(得分:0)
仅仅告诉我们这不起作用是不够的,我们没有水晶球来确定你的输入是什么,你期望什么,结果是什么。鉴于我们缺乏信息,我们的答案质量可能不会很高,因此,当我试图帮助你时,我只能依靠我所看到的。
我看到NEW.res_price
&lt; 0或&gt; 84000,触发器将执行一些插入并将proccessed
设置为1.稍后,检查proccessed
是否为0,这是假的,因为您已将proccessed
设置为1。 / p>
如果该值介于0到84000之间,则在满足以下条件之一的情况下将执行您的案例:
- dayOfWeek is '7' and EW.res_price > limitTop*priceMinWeekEnd
- dayOfWeek is '6' and NEW.res_price <> limitBottom*priceMinWeekEnd
- dayOfWeek differs from '6' and '7' and NEW.res_price <> limitBottom*priceMinWeekEnd