gpt4 book ai didi

mysql - 在同一张表上更新后在触发器中更新表

转载 作者:IT老高 更新时间:2023-10-29 00:02:49 25 4
gpt4 key购买 nike

在同一张表上更新后,如何在触发器中更新表的列?
这里是触发器:


CREATE TRIGGER upd_total_votes AFTER UPDATE ON products_score
FOR EACH ROW
UPDATE
products_score
SET
products_score.votes_total =
(SELECT
(votes_1 + votes_2 + votes_3 + votes_4 + votes_5)
FROM
products_score
WHERE
id = new.id)

现在当我更新表格时


UPDATE products_score SET votes_1 = 5 WHERE id = 0

这不起作用,因为我得到以下信息:

#1442 - Can't update table 'products_score' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.

那么我到底怎么才能让它工作呢?

最佳答案

如果您将触发器更改为 BEFORE 而不是 AFTER 您可以这样做:

CREATE TRIGGER upd_total_votes BEFORE UPDATE ON products_score 
FOR EACH ROW
BEGIN
SET new.votes_total = new.votes_1 + new.votes_2 + new.votes_3 + new.votes_4 + new.votes_5
END
;

关于mysql - 在同一张表上更新后在触发器中更新表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4547465/

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