douzinei6926 2016-07-04 12:49
浏览 26

用于更新PostCount的MySql触发器

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?

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.

comment table rows: id | cdate | edate | author | email |status | content | url | bid | level

blog table rows: id | cid | author | cdate | title | teaser | content | url

user table rows: id | username | email | pass | posts

  • 写回答

1条回答 默认 最新

  • dongya2030 2016-07-04 12:55
    关注

    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$$ 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用