插入和一段时间后更新列值 [英] update column value after insert and period of time
问题描述
我有一个包含 status
的表,第一次插入时默认值为 0
,我想将此值从 0
更改> 在此行插入后 24 小时后变为 1
,这意味着状态值在插入后 24 小时
后变为 1
.
I have a table including status
that default value is 0
when it inserted in the first time, I want to change this value from 0
to 1
after this row inserted in 24 hours, that means status value changed to 1
after 24 hours
from inserted.
使用 MySQL.我该怎么做?
Using MySQL. How can I do this?
推荐答案
这可能不是正确的方法.我的意思是,您可以设置一个为每一行处理的事件,但这可能会给您的数据库增加大量负载.
This is probably not the right approach. I mean, you could set up an event that gets processed for every row, but that could add a lot of load to your database.
相反,如果 status
只是说该行少于或多于一天,请将创建日期放入表中并使用视图:
Instead, if status
is merely saying that the row is less or more than one day old, put a creation date into the table and use a view:
create view v_table as
select t.*, (creation_date >= date_sub(now(), interval 1 day) as status
from table t;
如果 status
可以通过其他方式改变,那么调用它类似于 _status
并执行:
If status
can be changed by other means, then call it something like _status
and do:
create view v_table as
select t.*,
(case when creation_date >= date_sub(now(), interval 1 day then 1 else _status end) as status
from table t;
这篇关于插入和一段时间后更新列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!