简体   繁体   中英

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.

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 Ymd format used to indicate the date on which the associated code is active.

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!

I can use PHP to help with this, if I need to run any loops or anything.

With pure SQL:

set @i = unix_timestamp(date(now())) - 86400;
update thetable set code_date = date(from_unixtime(@i:=@i+86400)) order by id;

86400 is the number of seconds in a day.

$records = mysql_fetch_assoc(mysql_query(SELECT * FROM records_table));
$start_date = strtotime('now');

foreach ($records as $record)
       {
           $record['time'] = strtotime('+1 day' , $start_date = time());
           $start_date = $record['time'];

           mysql_query ("UPDATE records_table
                         SET time = $record['time']
                         WHERE id = $record['id']");
       }

My strtotime function construction might be a bit off, if someone could verify/edit but other then that I believe that would accomplish the task. Also if you need to order the rows then in the original assoc array query simply add "ORDER BY id desc" for example if you wanted to order the rows by the ID in descending order.

is this what you are looking for?

its a date_add function in MySql

SELECT `id` FROM `table` ORDER BY `id` ASC

Get the results of the above query and put it into an array, such as $ids .

Refer to this link I used to add days.

//$today = date('Y-m-d');

$sql = 'INSERT IGNORE INTO `table` (`id`, `code_date`)';
$sql .= 'VALUES ';

$values_array = array();
$i = 0;
foreach($ids as $id) {
  $values_array[] = '($id, "' . date('Y-m-d', strtotime('+' . $i . ' day') . '");
  $i++;
}
$sql .= implode(',' . "\n",$values_array);

$sql .= "\n" . 'ON DUPLICATE KEY UPDATE `code_date`=VALUES(`code_date`)';

Now you have have your query. Send it with mysql or PDO or whatever.

Eg

$result = mysql_query($sql);

Explanation (in case it's not obvious): 1.) You got all the existing ids with the SELECT query. 2.) You set $today variable with today's date. 3.) You construct INSERT query using foreach loop to increment the days for each id. 4.) You send the query.

Let me know if that works. It should, unless I made a typo or some other minor mistake. I did not test it.

This should work for you, theres probably a more efficient way, but this is how I would do it.

//Get the database values
     $q = mysql_query("SELECT * FROM yourtable");
     $res = mysql_fetch_array($q);
     $date = date('Y-m-d', strtotime('now'));

 //Or you can specify like $date = date('Y-m-d', strtotime('2013-06-14'));
 //Either way

     foreach ($res as $val)
   {   
       //update the row
       mysql_query ("UPDATE yourtable
                     SET code_date = $date
                     WHERE id = $val['id']")
       //Increment date forward..
       $date = date('Y-m-d', strtotime( '+1 day' , strtotime ($date) ));


   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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