gpt4 book ai didi

mysql - 这个用于排名的 MySQL 触发器有什么问题?

转载 作者:行者123 更新时间:2023-11-29 08:13:23 24 4
gpt4 key购买 nike

我正在尝试编写一个触发器,将表中的所有条目从 1 到 10 进行排名(最大值的排名为 10,最小值的排名为 1,所有其他条目都分配为介于两者之间的整数值)。触发代码如下:

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

DECLARE n_project_id integer; #project_id associated with risk
DECLARE max_cost double; #previous maximum expected_cost in project
DECLARE min_cost double; #previous minimum expected_cost in project
DECLARE max_impact double; #previous maximum impact_effect in project
DECLARE min_impact double; #previous minimum impact_effect in project
DECLARE slope double; #slope for prioritizing function

SELECT t.project_id INTO n_project_id FROM tasks t WHERE t.task_id = NEW.task_id; #GET PROJECT_ID ASSOCIATED WITH THE RISK

SET NEW.expected_cost = NEW.probability * NEW.cost_impact, NEW.overall_impact = NEW.probability * NEW.impact_effect; #CALCULATE EXPECTED_COST AND OVERALL_IMPACT FIELDS

SELECT MAX(expected_cost), MIN(expected_cost), MAX(overall_impact), MIN(overall_impact) INTO max_cost, min_cost, max_impact, min_impact FROM view_risks WHERE r.project_id = n_project_id; #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;
ELSEIF ((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;
ELSEIF (NEW.expected_cost > max_cost) THEN
SET NEW.priority_monetary = 10;
SET slope = 9 / (NEW.expected_cost - min_cost);
UPDATE risks SET priority_monetary = slope * (expected_cost - min_cost) + 1 WHERE project_id = n_project_id;
ELSE #NEW VALUE CORRESPONDS TO A MINIMUM
SET NEW.priority_monetary = 1;
SET slope = 9 / (max_cost - NEW.expected_cost);
UPDATE risks SET priority_monetary = slope * (expected_cost - min_cost) + 1 WHERE project_id = n_project_id;
END IF;

/*
Update Priority Effect Rankings
*/
IF (max_impact IS NULL OR min_impact IS NULL) THEN #check for empty table
SET NEW.priority_effect = 10;
ELSEIF ((NEW.overall_impact <= max_impact) AND (NEW.overall_impact >= min_impact)) THEN
#NEW VALUE DOES NOT CHANGE TABLE EXTREMES
IF (max_cost - min_cost = 0) THEN
SET NEW.priority_effect = 10;
ELSE
SET slope = 9 / (max_impact - min_impact);
SET NEW.priority_effect = slope * (NEW.overall_impact - min_impact) + 1;
END IF;
ELSEIF (NEW.overall_impact > max_impact) THEN
SET NEW.priority_effect = 10;
SET slope = 9 / (NEW.overall_impact - min_impact);
UPDATE risks SET priority_effect = slope * (overall_impact - min_impact) + 1 WHERE project_id = n_project_id;
ELSE #NEW VALUE CORRESPONDS TO A MINIMUM
SET NEW.priority_effect = 1;
SET slope = 9 / (max_impact - NEW.overall_impact);
UPDATE risks SET priority_effect = slope * (overall_impact - min_impact) + 1 WHERE project_id = n_project_id;
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 '' at line 5 

谁能解释一下这是怎么回事吗?供您引用,我的排名算法由以下带有常量 maxval 和 minval 的函数描述:

Rank(x) = 1 + 斜率(x - minval),其中斜率 = 9/(maxval - minval)。

谢谢!

其他信息:

字段类型:预期成本 => 小数(11,2)priority_monetary =>tinyint(2)

示例值:预期成本 => 1000.00priority_monetary => 2

最佳答案

您已经有了 ELSE IF,而您应该使用 ELSEIF。

正如 @Mihai 所写:你的缺点不会显示为缺点,并且 UPDATE 之后有关键字 TABLE (应该只有 UPDATE tableName)。

PS 我建议您使用一些MySql工具,例如MySql Workbench ,它有 Windows、大多数 Linux 系统和 OSX 的发行版。它会让你工作得更好并检查你的语法。

更改后的代码:

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;
ELSEIF ((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;
ELSEIF (NEW.expected_cost > max_cost) THEN
SET NEW.priority_monetary = 10;
SET slope = 9 / (NEW.expected_cost - min_cost);
UPDATE 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 risks SET priority_monetary = slope * (expected_cost - NEW.min_cost) + 1;
END IF;

END $$
DELIMITER ;

关于mysql - 这个用于排名的 MySQL 触发器有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21097659/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com