gpt4 book ai didi

mysql - 如何创建更新触发器以增加/减少 1 个数字到总票数

转载 作者:行者123 更新时间:2023-11-29 07:33:09 24 4
gpt4 key购买 nike

我有两个表:

// posts
+----+---------+-----------+-------------+
| id | title | content | total_votes |
+----+---------+-----------+-------------+
| 1 | title1 | content1 | 3 |
| 2 | title2 | content2 | 2 |
+----+---------+-----------+-------------+

// votes
+----+---------+-------+
| id | id_post | value |
+----+---------+-------+
| 1 | 1 | 1 |
| 2 | 1 | 1 |
| 3 | 1 | 1 |
| 4 | 2 | -1 |
| 5 | 2 | 1 |
| 6 | 2 | 1 |
| 7 | 2 | 1 |
+----+---------+-------+

现在我需要一个触发器来更新posts.total_votes。当一个用户给出新的投票(1或-1)时,它将成为votes表中的一个新行,所以我想在插入到votes表后,自动触发器更新 total_votes 数量并应用新的投票。这可能吗?

例如:

If new votes.values == 1  then posts.totla_votes++;
If new votes.values == -1 then posts.total_votes--;
<小时/>

编辑:

我有两个帖子表(posts_A | posts_B)。此外,我还在投票表上添加了一个新列,其中包含表的名称。所以我需要触发器更新适当的表。像这样的东西:update new.table_name ...而不是update posts ...

// posts_A
+----+---------+-----------+-------------+
| id | title | content | total_votes |
+----+---------+-----------+-------------+
| 1 | title1 | content1 | 2 |
| 2 | title2 | content2 | -1 |
+----+---------+-----------+-------------+

// posts_B
+----+---------+-----------+-------------+
| id | title | content | total_votes |
+----+---------+-----------+-------------+
| 1 | title1 | content1 | 1 |
| 2 | title2 | content2 | 3 |
+----+---------+-----------+-------------+

// votes
+----+---------+-------+------------+
| id | id_post | value | table_name |
+----+---------+-------+------------+
| 1 | 1 | 1 | post_A |
| 2 | 1 | 1 | post_A |
| 3 | 1 | 1 | post_B |
| 4 | 2 | -1 | post_A |
| 5 | 2 | 1 | post_B |
| 6 | 2 | 1 | post_B |
| 7 | 2 | 1 | post_B |
+----+---------+-------+------------+

这是我的尝试,但我不知道为什么它不起作用? :

delimiter //
create trigger total_votes_count_upd after update on votes
for each row
begin
if (new.value == 1) then
update new.table_name set total_votes = total_votes+1
where id = new.id_post;
elseif (new.value == -1) then
update new.table_name set total_votes = total_votes-1
where id = new.id_post;
end if;
end;//

delimiter //

实际上我直接替换了new.table_name而不是表的名称(posts)。但正如我所说,它不起作用。我该如何修复它?

最佳答案

是的,您需要为此创建一个插入后触发器

delimiter //
create trigger total_votes_count after insert on votes
for each row
begin
if (new.value == 1) then
update posts set total_votes = total_votes+1
where id = new.id_post;
elseif (new.value == -1) then
update posts set total_votes = total_votes-1
where id = new.id_post;
end if;
end;//

delimiter //

为了处理更新,一切都保持不变,只是您需要另一个触发器

delimiter //
create trigger total_votes_count_upd after update on votes
for each row
begin
if (new.value == 1) then
update posts set total_votes = total_votes+1
where id = new.id_post;
elseif (new.value == -1) then
update posts set total_votes = total_votes-1
where id = new.id_post;
end if;
end;//

delimiter //

由于您有 2 个帖子表,因此您需要在 if 条件中使用它

delimiter //
create trigger total_votes_count after insert on votes
for each row
begin
if (new.value == 1) then
if (new.table_name == 'post_A') then
update posts_A set total_votes = total_votes+1
where id = new.id_post;
else
update posts_B set total_votes = total_votes+1
where id = new.id_post;
end if;
elseif (new.value == -1) then
if (new.table_name == 'post_A') then
update posts_A set total_votes = total_votes-1
where id = new.id_post;
else
update posts_B set total_votes = total_votes-1
where id = new.id_post;
end if ;
end if;
end;//

delimiter //

对更新触发器执行相同的操作。

关于mysql - 如何创建更新触发器以增加/减少 1 个数字到总票数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31920600/

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