mysql - Automatically update occurrences in another table(mysql - 自动更新另一个表中的事件)
问题描述
我有两张表,一张用于存储用户的报告,另一张用于存储用户.
I have two tables, one for storing reports of a user, and another for storing users.
1. Report_table
-----report_id(P)-----user_id--------
001 user1
002 user1
003 user3
2.Users table
-------user_id(P)-------no_of_reports
user1 2
user2 0
user3 1
现在,每当在 report_table 中插入报告时(比如 report_id:004,user_id:user1),我都会像这样更新两个表.
Now whenever a report is inserted in report_table(say report_id:004, user_id: user1), I update both tables like this.
INSERT INTO reports_table VALUES (004,`user1`)
INSERT INTO users_table (user_id,no_of_reports) VALUES (`user1`,1)
ON DUPLICATE KEY UPDATE no_of_reports=no_of_reports+1;
有没有更好的方法来做到这一点..有没有办法在插入新报告时自动递增 no_of_reports 计数器?
Is there a better way of doing this.. Is there a way of automatically incrementing the no_of_reports counter when a new report is inserted?
推荐答案
你可以使用AFTER INSERT
触发器来处理TRIGGER
You can deal with TRIGGER
by using a AFTER INSERT
trigger
http://dev.mysql.com/doc/refman/5.0/en/triggers.html
这篇关于mysql - 自动更新另一个表中的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:mysql - 自动更新另一个表中的事件


基础教程推荐
- SQL Server 2016更改对象所有者 2022-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01