gpt4 book ai didi

MYSQL触发器仅在运行至少两次后更新

转载 作者:行者123 更新时间:2023-11-29 10:54:14 25 4
gpt4 key购买 nike

我的问题是,我有有效的触发器。我有 2 张 table :

  • 节点
  • 表b

触发器内的查询有效,所以我认为我弄乱了触发器,而且我不知道我做错了什么(之前从未使用过MySQL,所以对我来说这是一个猜谜游戏)

每当我删除tableb和node表,并插入到node中时,新值不会被计入tableb,只有当我再次插入到node中时。然后一切正常。我的MySQL触发器如下:

DELIMITER //
CREATE TRIGGER INSERT_USERS_FORUM
AFTER INSERT ON node
FOR EACH ROW BEGIN

update tableb
set forum_count = (select count(*) from node n, forum_index fi
where n.nid = fi.nid and fi.tid = 18
and n.uid = new.uid
group by n.uid)
where UID = new.uid;
END//

感谢任何帮助,提前致谢

最佳答案

通过在tableb中的uid上设置唯一键,并使用INSERT INTO ... ON DUPLICATE KEY UPDATE,您可以确保一行存在于 tableb 中以保存论坛计数。

DELIMITER //

CREATE TRIGGER INSERT_USERS_FORUM
AFTER INSERT ON node
FOR EACH ROW BEGIN
INSERT INTO `tableb`
(`forum_count`)
VALUES
((select count(*) from `node` n, `forum_index` fi
where n.nid = fi.nid and fi.tid = 18
and n.uid = new.uid
group by n.uid))
ON DUPLICATE KEY UPDATE `forum_count` = (select count(*) from `node` n, `forum_index` fi
where n.nid = fi.nid and fi.tid = 18
and n.uid = new.uid
group by n.uid)
where `UID` = new.`uid`;
END//

DELIMITER ;

关于MYSQL触发器仅在运行至少两次后更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43259651/

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