我试图写一个触发器,将表中的所有条目排列成从1到10(最大值具有秩10,最小值具有秩1,所有其他值都被分配在整数值之间)。以下是触发代码:

DELIMITER $$
CREATE TRIGGER risks_before_insert
BEFORE INSERT ON risks
FOR EACH ROW
BEGIN

DECLARE max_cost double; #previous maximum expected_cost in project
DECLARE min_cost double; #previous minimum expected_cost in project
DECLARE slope double; #slope for prioritizing functioN

SELECT MAX(expected_cost), MIN(expected_cost) INTO max_cost, min_cost FROM view_risks; #GET EXTREME VALUES FROM TABLE, STORE IN MEMORY

/*
Update Priority Monetary Rankings
*/
IF (max_cost IS NULL OR min_cost IS NULL) THEN #check for empty table
    SET NEW.priority_monetary = 10;
ELSE IF ((NEW.expected_cost <= max_cost) AND (NEW.expected_cost >= min_cost)) THEN #NEW VALUE DOES NOT CHANGE TABLE EXTREMES
    IF (max_cost – min_cost = 0) THEN
        SET NEW.priority_monetary = 10;
    ELSE
        SET slope = 9 / (max_cost – min_cost);
        SET NEW.priority_monetary = slope * (NEW.expected_cost - min_cost) + 1;
    END IF;
ELSE IF (NEW.expected_cost > max_cost) THEN
    SET NEW.priority_monetary = 10;
    SET slope = 9 / (NEW.expected_cost – min_cost);
    UPDATE TABLE risks SET priority_monetary = slope * (expected_cost - min_cost) + 1;
ELSE #NEW VALUE CORRESPONDS TO A MINIMUM
    SET NEW.priority_monetary = 1;
    SET slope = 9 / (max_cost – NEW.expected_cost);
    UPDATE TABLE risks SET priority_monetary = slope * (expected_cost – NEW.min_cost) + 1;
END IF;
END $$
DELIMITER ;

但是,我得到以下错误:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '– min_cost); SET NEW.priority_monetary = slope * (NEW.expected_cost - min_cost' at line 22

有人能解释什么不对吗?为了供您参考,我的排名算法由以下带有常量maxval和minval的函数描述:
秩(x)=1+斜率(x-minval),其中斜率=9/(maxval-minval)。

最佳答案

您应该检查是否存在超出范围的异常。它不应该等于0。如果它这样做,查询将失败并连接数据库。
在将maxval设置为minval之前,您应该检查maxval - minval0以确保slope不等于9/(maxval - minval)
如果你运行这个

SET slope = 9 / (max_cost – min_cost);

它试图将slope设置为9/0它将崩溃。

10-08 08:37