mysql:触发器创建时无法识别新关键字

时间:2019-02-18 10:19:52

标签: mysql triggers mysql-5.7

致力于mysql.5.7

这是我的bugs桌子

MySQL [jira_statistics]> describe bugs;
+---------------------------------------+--------------+------+-----+---------+-------+
| Field                                 | Type         | Null | Key | Default | Extra |
+---------------------------------------+--------------+------+-----+---------+-------+
| issue_key                             | varchar(45)  | NO   | PRI | NULL    |       |
| release_name                          | varchar(45)  | YES  | MUL | NULL    |       |
| issue_summary                         | varchar(200) | YES  |     | NULL    |       |
| story_points                          | int(11)      | NO   |     | 0       |       |
| qa_reopened                           | float        | NO   |     | 0       |       |
| done_reopened                         | float        | NO   |     | 0       |       |

此表通过定期调用LOAD DATA LOCAL INFILE bugs <file.csv>

来更新

无论何时进行此更新(它可能会更新现有行和/或插入新行),我都希望通过以下触发器来更新具有某些产生的统计信息的另一个表

create trigger update_bugs_stats after insert on `jira_statistics`.`bugs` for each row

begin

delimiter ;

 -- STORY POINTS -------------------------

SELECT AVG(story_points) INTO @avg_bugs_storypoints  FROM `jira_statistics`.`bugs` WHERE release_name = new.release_name;
SELECT MAX(story_points) INTO @max_bugs_storypoints FROM `jira_statistics`.`bugs` WHERE release_name = new.release_name;
SELECT MIN(story_points) INTO @min_bugs_storypoints  FROM `jira_statistics`.`bugs` WHERE release_name =  new.release_name;

INSERT INTO storypoints_stats (release_name, avg_bugs_storypoints, max_bugs_storypoints, min_bugs_storypoints)
VALUES (relName, @avg_bugs_storypoints, @max_bugs_storypoints, @min_bugs_storypoints)
ON DUPLICATE KEY UPDATE 
    relName=new.release_name,
    avg_bugs_storypoints=@avg_bugs_storypoints,
    max_bugs_storypoints=@max_bugs_storypoints,
    min_bugs_storypoints=@min_bugs_storypoints;

但是,每当尝试创建触发器时,都会给我以下错误:

  

new.release_name子句中的未知列where

为什么无法识别new关键字?

2 个答案:

答案 0 :(得分:0)

因为new被保留为系统字

参考:https://dev.mysql.com/doc/refman/8.0/en/keywords.html

请修改

new.release_name  ==> `new`.`release_name`

等等。

答案 1 :(得分:0)

错误比我想象的还要愚蠢;

我直接在sql查询编辑器上工作,而不是在mysql工作台的“触发器”选项卡上工作,因此它无法正确解析new关键字`。