网络营销电子商务研究中心  
How to buy the best prescription safety glasses in Canada? Let's study!
Go Back   网络营销电子商务研究中心 > 服务器与数据库 > MySQL
User Name
Password
 
FAQ Members List Calendar Cheap Glasses Mark Forums Read

Reply
 
Thread Tools Display Modes
  #1   IP: 112.87.30.158
Old 2015-04-19, 06:17 AM
Illiopolis Illiopolis is offline
初级会员
 
Join Date: 2006-09-30
Posts: 1
Illiopolis 现在声名狼藉
Default Insert/ Update random date in MySQL

How would I update a column with a random date in the past 2 weeks using MySQL?

For example (code doesn't actually work):
Code:
UPDATE mytable
SET col = sysdate()-rand(1,14);
Reply With Quote
  #2   IP: 112.87.30.158
Old 2015-04-19, 06:18 AM
Iker Iker is offline
初级会员
 
Join Date: 2008-09-20
Posts: 1
Iker 现在声名狼藉
Default

24

down vote

accepted


You can get a random integer with this expression:


To obtain a random integer R in the range i <= R < j, use the expression FLOOR(i + RAND() * (j - i)). For example, to obtain a random integer in the range the range 7 <= R < 12, you could use the following statement:
Code:
SELECT FLOOR(7 + (RAND() * 5));
http://dev.mysql.com/doc/refman/5.1/...functions.html

Use that to generate a random number of days, hours or minutes (depending on the resolution) and add that number to current date. Full expression would be something like this:
Code:
SELECT NOW() - INTERVAL FLOOR(RAND() * 14) DAY;
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Mysql insert random datetime in a given datetime range Imperial MySQL 1 2015-04-19 06:16 AM
Random MySQL date Kimberling City MySQL 1 2015-04-18 11:09 PM
MySQL Generate Random Date Kensington MySQL 0 2015-04-18 11:02 PM
MySQL毫秒值和日期的指定格式的相互转换 Kaitlin MySQL 0 2015-04-18 11:01 PM
mysql 帮助文档使用 Haddam MySQL 0 2014-11-26 06:43 PM

Prescription-glasses.com offers prescription glasses online at discount prices.
All times are GMT +8. The time now is 02:05 AM.


Powered by vBulletin Version 3.8.7
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.