如何创建更新/减少1号码到总票数的更新触发器

时间:2015-08-10 13:15:53

标签: mysql

我有两张桌子:

// 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表的数量{1}}并申请新的投票。有可能吗?

例如:

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)的名称。但正如我所说,它不起作用。我该如何解决?

1 个答案:

答案 0 :(得分:2)

是的,您需要为此

创建after insert trigger
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 //

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