开发者

MYSQL Subctract Days and Seconds

Greeting all

I 开发者_Python百科update my field 'createdOn' datetime field to hold values of last 7 days in random like this:

UPDATE posts
   SET createdOn
     = DATE_SUB(DATE(NOW()), INTERVAL ROUND(RAND()*7) DAY)

Although this gives me the random dates I need, the hours, minutes and seconds are not random and are like 00:00:00. How can I include random hours, minutes and seconds also in the above? Unique hours, minutes and seconds in the last seven days would be even better.

Thanking you


Here it is an example of random hours, minites and seconds in the last seven days -

UPDATE posts
  SET createdOn = DATE(NOW()) - INTERVAL FLOOR(RAND() * 604800) SECOND;

Where 604800 = 60 seconds * 60 minutes * 24 hours * 7 days.


You're casting NOW() as a DATE, which will strip out the time portion, and force it to 00:00:00. Perhaps do another RAND() on TIME(NOW()) and adding them together.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜