I have the following trigger in which CASE statements are not executed:
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;
The table structure is:
Table exp_hotel:
hot_id | hot_webid | hot_name |hot_starrating | hot_brandbool
7733871| 475 | Richwood Garden Hotel|0 | 0
Table exp_query:
que_id | que_city | que_children | que_adults | que_week | que_day | que_staylength
13 | London | 0 | 2 | 2 | 6 | 1
Table 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
Table exp_alert:
ale_id | ale_res_id | ale_proccessed
(Auto Inc.)
So now if I insert this line in 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
The trigger is perfectly working in the case where NEW.res_price is < 0 or > 84000 when I am setting the res_price to 0 or 84000, but for some reasons it refuses to execute the code which is in all CASE statements when I put a lower price (for instance 100).
I have stored all the variables I use in a test table and all their values are correct (note that either priceMinWeekDay or priceMinWeekEnd is NULL, this is normal).
It is not enough to tell us that is not working, we do not have crystal balls to determine what is your input, what do you expect instead and what is the result. Given the fact that we lack information, the quality of our answers might be not very high, therefore, as I try to help you I can only rely on what I see.
I see that in the case when NEW.res_price
< 0 or > 84000, the trigger will execute some inserts and sets proccessed
to 1. Later on, you check whether proccessed
is 0, which is false, as you have already set proccessed
to 1.
If the value is between 0 and 84000, your cases will execute if one of the following criteria is met:
- 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.