View Single Post
  #3   IP: 112.87.30.158
Old 2015-04-18, 11:06 PM
Kiefer Kiefer is offline
初级会员
 
Join Date: 2009-09-02
Posts: 1
Kiefer 现在声名狼藉
Default

Here’s yet another way:

Code:
UPDATE tableName
 SET column = FROM_UNIXTIME(RAND() * (UNIX_TIMESTAMP(‘2015-7-20 15:00:00′) – UNIX_TIMESTAMP(‘2008-09-10 12:00:00′)) + UNIX_TIMESTAMP(‘2008-09-10 12:00:00′));
This will set the column’s value to a random datetime between ‘2015-7-20 15:00:00′ and ‘2008-09-10 12:00:00′.

If you only have a DATE field, you can extract the date from the end result. For example:

Code:
UPDATE tableName
 SET column = DATE(FROM_UNIXTIME(RAND() * (UNIX_TIMESTAMP(‘2015-7-20 15:00:00′) – UNIX_TIMESTAMP(‘2008-09-10 12:00:00′)) + UNIX_TIMESTAMP(‘2008-09-10 12:00:00′)));
The time part of the value is not required, so one could do UNIX_TIMESTAMP(‘2025-01-05′) without worry.

Good luck!
Reply With Quote