0

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.

1
  • Can you give us the schema (DESC of the table(s) in question)? Commented Aug 16, 2011 at 16:16

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.