【发布时间】:2015-10-06 00:31:03
【问题描述】:
我想创建一个事件,当 lend_date 列正好经过 15 天时,它将执行 INSERT 查询。
它将获取该行的 ID 和userid,并将其插入到另一个表中。
例如:
id | userid | lend_date
---+----------+----------------------
1 | 1 | 2015-09-24 15:58:48
2 | 1 | 2015-09-21 08:22:48
而现在,正是2015-10-06 08:23:41。所以事件应该得到第二行的ID,即2,并插入到另一个表中。
我的事件查询应该是什么样的?
事件类型是RECURRING。但我也不确定我应该每小时还是每天执行一次。最好的建议是什么?
这比使用Task Scheduler 更好吗?
我想插入获取的ID的另一个表是notification_table,它会通知用户他/她有一个过期日期。
notification_table 看起来像这样:
id | userid | notificationid | notificationdate |
---+----------+------------------+----------------------+
1 | 1 | 1 | 2015-09-24 15:58:48 |
2 | 1 | 1 | 2015-09-21 08:22:48 |
我正在查看这个查询:
INSERT INTO notification_table (id, userid, notificationid, notificationdate)
SELECT id, userid, 1, NOW()
FROM first_table
WHERE lend_date + INTERVAL 15 DAY = NOW();
【问题讨论】:
-
正如@Strawberry 所说,你真的需要这个吗?有什么特殊原因导致您不能只使用 where 子句进行查询以从现有表中获取数据?
标签: mysql scheduled-tasks mysql-event