简体   繁体   English

倒数mysql数据库中的字段值

[英]count down field value in mysql database

I have a field that hold integer for number of day.the number of this field is Between 1-6 .i want to make count down this field after a day.for example if the value of this field is 4 after one day the value of this field must change to 3 and after two day value of this field must change to 2.and when 4 day is over this field must delete . 我有一个字段保存天数的整数。此字段的数目在1-6之间。我想在一天后对该字段进行递减计数,例如如果此字段的值在一天后为4此字段的值必须更改为3,并且两天后该字段的值必须更改为2。当超过4天时,必须删除此字段。 What should I do? 我该怎么办?

An alternative approach is to store the date when the field was first set and the count. 另一种方法是存储首次设置该字段的日期和计数。 Then you can get the count on any particular day using a view: 然后,您可以使用视图获取任何特定日期的计数:

select t.*,
       greatest(0, cnt - datediff(date(now()), SetDate))) as currentvalue
from table t
having currentvalue > 0;

You don't actually have to delete the value from the table. 您实际上不必从表中删除该值。 It might be useful in the future for auditing purposes (unless the table is big, so reducing the size is actually useful). 将来可能对审计目的很有用(除非表很大,所以减小大小实际上很有用)。

you can do this by three ways as , 您可以通过三种方式来做到这一点,

1.Mysql Event scheduler 2.Cronjob 3.jquery setInterval() 1.Mysql事件调度程序2.Cronjob 3.jquery setInterval()

But the best way to do is cornjob because, for event scheduler you need mysql server connection always open.And jquery setinterval() function will be called only when you refresh or loads the page were the setinterval() is called. 但是最好的方法是cornjob,因为对于事件调度程序,您需要始终打开mysql服务器连接。并且只有在刷新或加载页面时调用jquery setinterval()函数才能调用setinterval()。

使用cron作业计划此脚本。

UPDATE table SET day = (day - 1) WHERE your condition;

Doesn't sound like a very sensible way to approach this. 听起来不是解决此问题的明智方法。 I assume you want to have, for example, a record that is automatically deleted after a while. 我假设您想拥有例如一段时间后会自动删除的记录。 Why don't you add a timestamp at which it expires? 为什么不添加过期的时间戳? You can then either setup a scheduled job, as suggested by the commenters, or just run a query to DELETE FROM ... WHERE expiration_timestamp <= NOW() - for example - when the PHP script runs. 然后,您可以按照评论者的建议设置排定的作业,或者仅运行查询以DELETE FROM ... WHERE expiration_timestamp <= NOW() DELETE FROM ... WHERE -例如-运行PHP脚本时。 This does not guarantee it gets run at most / at least once every day, but it does guarantee that the data is up to date when you access it. 这不能保证它最多每天运行一次,至少每天运行一次,但是可以保证访问数据时数据是最新的。

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

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