Reputation: 3752
I have a mysql table called books that has a "votes" column, and another table called series which has a "total_votes" column. The votes column tracks votes for an individual book, while the total_votes column is a tabulation of all votes for all books within a series.
In the administration section, an admin can manually change the number of votes an individual book has. I want to create a mysql trigger so that whenever a vote is changed, the difference between old vote value and new vote value is reflected in the total_votes column. For example, if Book A has 10 votes and Series 1 has a total of 100 votes, when I change the vote count of Book A to 5, I want the total_vote count of Seris 1 to be 95.
I understand how to make a basic trigger, but I'm not sure how update the Series table with the difference between the old and new vote values.
TIA.
Upvotes: 0
Views: 134
Reputation: 88064
Don't worry about the difference, just update based on a new sum. For example:
(Not sure if this is the exact MySql syntax)
update series
set total_votes = sum(select votes from books b where b.seriesid = seriesid)
Upvotes: 1