gpt4 book ai didi

mysql - 触发帮助函数(MySQL)

转载 作者:行者123 更新时间:2023-11-30 23:43:09 25 4
gpt4 key购买 nike

我正在使用 MySQL 5.0.32。

我有两个表:UserItemVoteItemStat

UserItemVote 有:

user_id, item_id, vote(tinyint either 0 for downvote or 1 for upvote)

ItemStat 有:

item_id, total_up_vote, total_down_vote

当用户投票赞成/反对一个项目时(插入到 UserItemVote 表),我想要一个触发器 tai_UserItemVote 来更新total_up_votetotal_down_vote 来自 ItemStat

到目前为止,我只解决了更新触发器,没有解决插入触发器。

tai_UserItemVote 插入触发器:

// insert to itemstat if row not exist otherwise update itemstat.

tau_UserItemVote 更新触发器:

UPDATE ItemStat set total_down_vote=total_down_vote+1 
WHERE item_id=NEW.item_id and NEW.vote = 0;
UPDATE ItemStat set total_up_vote=total_up_vote+1
WHERE item_id=NEW.item_id and NEW.vote = 1;

最佳答案

INSERT ... ON DUPLICATE KEY UPDATE应该做你需要的。

关于mysql - 触发帮助函数(MySQL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/974515/

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