触发器中的 Mysql 案例不起作用

标签 mysql triggers case

我有以下触发器,其中不执行 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

当我将 res_price 设置为 0 或 84000 时,在 NEW.res_price 为 < 0 或 > 84000 的情况下,触发器完美工作,但由于某些原因,当我将其设置为 0 或 84000 时,触发器拒绝执行所有 CASE 语句中的代码。输入较低的价格(例如 100)。

我已将我使用的所有变量存储在测试表中,并且它们的所有值都是正确的(请注意,priceMinWeekDay 或 PriceMinWeekEnd 为 NULL,这是正常的)。

最佳答案

仅仅告诉我们这不起作用是不够的,我们没有 Crystal 球来确定您的输入是什么、您期望什么以及结果是什么。由于我们缺乏信息,我们的答案质量可能不是很高,因此,当我试图帮助您时,我只能依靠我所看到的。

我看到,在 NEW.res_price < 0 或 > 84000 的情况下,触发器将执行一些插入并将 processed 设置为 1。稍后,您检查processed 是否为 0,这是 false,因为您已经将 processed 设置为 1。

如果该值介于 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

关于触发器中的 Mysql 案例不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23309070/

相关文章:

scala - 无限循环Scala代码

java - 我在 java 中执行更新查询时遇到 Sql 语法错误( java.sql.sqlsyntaxerrorexception 你的 sql 语法有错误)

mysql - Sql触发器的查询

mysql - 选择case when + join, MYSQL

mysql - 一个MySQL触发器中包含多个事件

c# - 如何根据日期自动运行 SQL 查询?

mysql - SQL作为IF语句的结果返回一个字符串

MySQL:子查询 WHERE IN(连接的结果)

sql - Mysql COUNT(*) 在多个表上

mysql - MySQL 队列是否插入/更新?