简体   繁体   English

如何每隔1小时自动运行mysql查询?

[英]How I can run mysql query automatically in every 1 hour?

I've integer value in my database and I want to decrease it with every passing hour. 我的数据库中有整数值,我想每隔一个小时就减小它。 How can I do it? 我该怎么做?

You don't need to decrement the value. 您不需要递减值。 You can just store the base value and create a view to calculate the value one the fly. 您可以只存储基本值并创建视图以即时计算该值。 Something like this: 像这样:

create table hourly (
    base_value int,
    starttime datetime
);

Then: 然后:

create view v_hourly
    select base_value - timestampdiff(hour, starttime, now())
    from hourly;

The alternative is to set up a job that runs each hour to decrement the value, but that is not necessary. 另一种方法是设置一个每小时运行一次的作业以减少该值,但这不是必需的。

You could use the mysql event scheduler. 您可以使用mysql事件调度程序。 You can find more information and examples here: https://dev.mysql.com/doc/refman/5.7/en/create-event.html 您可以在此处找到更多信息和示例: https : //dev.mysql.com/doc/refman/5.7/en/create-event.html

if you have used MySQL then please create cronJOB. 如果您使用过MySQL,请创建cronJOB。 if you have used SQL then please create Scheduler. 如果您使用过SQL,请创建Scheduler。

And set time and execute stored procedure. 并设置时间并执行存储过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM