我在MySQL数据库中创建了以下触发器。这是一个星级评分系统。无论何时将行添加到收视表中,都应该将其添加到ratings_totals表中。MySQL触发器不更新表
DELIMITER$$
create trigger update_vote_after_insert_trig before insert
on ratings
for each row begin
if bookID = new.bookID then
update ratings_totals set
total_votes = total_votes + 1,
total_rating = total_rating + new.rating,
overall_rating = total_rating/total_votes;
where
bookID = new.bookID;
ELSE
insert into ratings_totals set bookID = new.bookID, total_votes = total_votes, total_rating = total_rating, overall_rating = overall_rating;
END IF;
END$$
DELIMITER ;
我的问题是不正确地更新了ratings_totals表。它包含四行:bookID,total_votes,total_rating和overall_rating。只有bookID表被更新;其余的保持为0.
我的目标是在第一次插入行并在此之后进行更新。我做对了吗?
你为什么要插入然后更新?你为什么不立即插入? – 2012-02-19 14:30:12
因为在第一次插入后,我只想更新相同的行(即每个项目被评为1行)。我不想每次都为同一个项目插入一个新行;这是没有道理的。 – 2012-02-19 14:32:20
我已经取出插入线,但没有区别。触发器仍然不起作用。 – 2012-02-19 14:33:05