MySQL-如何将连续日期添加到许多现有记录中? [英] MySQL - How can I add consecutive dates to many existing records?
问题描述
我希望这很容易自己解决,但显然我需要帮助.我也无法在线找到答案,所以我在这里问.也许我只是没有使用正确的搜索词.
I was hoping this would be easy enough to figure out on my own but apparently I need help. I can't find the answer online either so I'm asking here. Perhaps I'm just not using the right search terms.
我有一个包含1100条现有记录的数据库表.共有3个字段:id,code_name,code_date. code_date是Y-m-d格式的日期,用于指示相关代码处于活动状态的日期.
I have a database table with 1100 existing records. There are 3 fields: id, code_name, code_date. The code_date is a date in Y-m-d format used to indicate the date on which the associated code is active.
我要做的就是从今天的日期开始,自动填充每个记录以及每个连续的日期.因此,第一行将是2013-06-14,下一行将是2013-06-15,依此类推.
All I want to do is autofill every record with each consecutive date starting with today's date. So, first row will be 2013-06-14, the next row will be 2013-06-15, and so on.
我觉得这应该非常简单,但显然我今天是个白痴.帮助将不胜感激!
I feel like this should be incredibly simple but apparently I'm an idiot today. Help would be appreciated!
如果我需要运行任何循环或任何东西,我可以使用PHP来帮助解决此问题.
I can use PHP to help with this, if I need to run any loops or anything.
推荐答案
使用纯SQL:
set @i = unix_timestamp(date(now())) - 86400;
update thetable set code_date = date(from_unixtime(@i:=@i+86400)) order by id;
86400
是一天中的秒数.
这篇关于MySQL-如何将连续日期添加到许多现有记录中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!