MySQL trigger: Update when reaching a certain datetime(MySQL触发器:到达特定日期时间时更新)
问题描述
我想创建一个 MySQL 触发器,每当另一个表中的一个日期时间行到达比现在低的日期时间时,它就会更新一个表.
I want to create a MySQL trigger that updates a table everytime one of the datetime rows in a different table reaches a datetime lower than now.
我将如何做到这一点?这可能吗?
举例说明:
table_1 table_2
-------- ------------------- -------- -
id 1 id 1
datetime 2011-05-10 11:11:11 counter 1
所以,当时间过去并且 NOW() 变成 2011-05-10 11:11:12 时,我想让计数器加 1.
So, when time passes and NOW() becomes 2011-05-10 11:11:12, then I want to have the counter upped by 1.
推荐答案
您应该能够使用触发器和事件调度程序来做到这一点:
- 在每次更新/插入时触发的表上创建一个触发器
- 此触发器创建在行的日期时间发生的预定事件并更新您的第二个表
You should be able to do it using a trigger and the event scheduler:
- create a trigger on the table that is fired on every update / insert
- this trigger creates a scheduled event that occurs at the datetime of the row and updates your second table
这篇关于MySQL触发器:到达特定日期时间时更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MySQL触发器:到达特定日期时间时更新
基础教程推荐
- 是否可以执行按位分组功能? 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 无法解决整理冲突 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
