View Single Post
  #1   IP: 112.87.30.158
Old 2015-04-18, 11:03 PM
Kersey Kersey is offline
初级会员
 
Join Date: 2007-02-13
Posts: 1
Kersey 现在声名狼藉
Default Add Random Dates to MySQL Database

Looking for a way to add random dates to a column in one of your MySQL tables?

I ran across this problem and found a straightforward solution. By utilizing str_to_date(), concat(), floor() and rand() you can specify a random date for a column.
Code:
update  mytable
set     mycolumn = str_to_date(
                               concat(
                                      floor(1 + rand() * (12-1)), 	/* Generate a random month */
                                      '-',
                                      floor(1 + rand() * (28 -1)), 	/* Generate a random day */
                                      '-',
                                      '2008'
                                      ),
                                '%m-%d-%Y'
                                );
Reply With Quote