用于更新PostCount的MySql触发器 [英] MySql Trigger for Updating PostCount
问题描述
我有三个数据表:comment,blog和user.我对MySql中的Triggers知之甚少,但是当插入或删除新的Row时,是否有可能总结注释和博客中每个Row的计数?
I have three datatables: comment, blog and user. I don't know much about Triggers in MySql, but is it possible to sum up the counts of every Row in comment and blog when a new Row is inserted or deleted?
例如博客有3个条目,评论有5个条目.当我现在删除一条评论时,它应该通过计算博客中的条目和用户名的评论来将用户中的记录"post"更新为7.
E.g. blog has 3 entries and comment has 5 entries. When I delete one comment now, it should update the record 'post' in user to 7 by counting the entries in blog and comment by the username.
注释表行:id | cdate |食用|作者|电子邮件|状态|内容|网址|出价|级别
comment table rows: id | cdate | edate | author | email |status | content | url | bid | level
博客表行:id | cid |作者| cdate |标题|预告片|内容|网址
blog table rows: id | cid | author | cdate | title | teaser | content | url
用户表行:id |用户名|电邮|通过帖子
user table rows: id | username | email | pass | posts
推荐答案
示例触发注释更新事件,您可以为所有其他事件更改更突出的触发器编写突出显示的值
EXAMPLE TRIGGER FOR comments update event you can write slimier triggers for all other events change highlighted values
DROP TRIGGER IF EXISTS comment_delete;
DELIMITER $$
CREATE TRIGGER comment_delete AFTER DELETE ON comment FOR EACH ROW BEGIN
UPDATE user set posts=posts-1 WHERE id=new.author;
END$$
这篇关于用于更新PostCount的MySql触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!