mysql中的触发器插入多行而不是一行

时间:2019-02-18 17:48:22

标签: mysql triggers mysql-5.7

我试图将来自4个不同表的4列总和插入到一个单独的表(名为bugs)触发器中,如下所示:

SELECT 
    uat_reopened_stats.release_name,
    (uat_reopened_stats.rate_bugs + qa_reopened_stats.rate_bugs + closed_reopened_stats.rate_bugs + done_reopened_stats.rate_bugs)
INTO @releasename, @bugstotalreopenrate  
FROM qa_reopened_stats
JOIN uat_reopened_stats 
    ON qa_reopened_stats.release_name = uat_reopened_stats.release_name
JOIN closed_reopened_stats 
    ON closed_reopened_stats.release_name = uat_reopened_stats.release_name
JOIN done_reopened_stats 
    ON done_reopened_stats.release_name = uat_reopened_stats.release_name;

INSERT INTO release_stats (release_name, bugs_total_reopen_rate)
VALUES (@releasename, @bugstotalreopenrate)
ON DUPLICATE KEY UPDATE 
    release_name=@releasename,
    bugs_total_reopen_rate=@bugstotalreopenrate;
END

但是,当执行触发器时(事先将release_stats表清空),最后我得到的行有11(!)行!

MySQL [jira_statistics]> select * from release_stats;
+--------------+------------------------+
| release_name | bugs_total_reopen_rate |
+--------------+------------------------+
| 2.3.0        | 9.090909004211426      |
| 2.3.0        | 9.090909004211426      |
| 2.3.0        | 9.090909004211426      |
| 2.3.0        | 9.090909004211426      |
| 2.3.0        | 9.090909004211426      |
| 2.3.0        | 9.090909004211426      |
| 2.3.0        | 9.090909004211426      |
| 2.3.0        | 9.090909004211426      |
| 2.3.0        | 9.090909004211426      |
| 2.3.0        | 9.090909004211426      |
| 2.3.0        | 9.090909004211426      |
+--------------+------------------------+

为什么会这样?

1 个答案:

答案 0 :(得分:0)

由于某种原因,在将release_name设置为表release_stats的主键后,问题消失了。