在MySQL中插入/更新随机日期 [英] Insert/ Update random date in MySQL
问题描述
例如(代码实际上不起作用):
更新mytable
SET col = sysdate() - rand(1,14);
您可以使用此表达式获得一个随机整数:为了获得
范围内的随机整数R i <= R
j,使用表达式
FLOOR(i + RAND()*(j - i))
。对于
示例,要获得
中的随机整数,范围7 <= R < 12,你
可以使用以下语句:SELECT FLOOR(7 +(RAND()* 5)) ;
http://dev.mysql.com/doc/refman/5.1/en/mathematical-functions.html
使用它来生成随机数的天数,小时或分钟(取决于分辨率),并将该数字添加到当前日期。完全表达式将是这样的:
SELECT NOW() - INTERVAL FLOOR(RAND()* 14)DAY;
How would I update a column with a random date in the past 2 weeks using MySQL?
For example (code doesn't actually work):
UPDATE mytable
SET col = sysdate() - rand(1, 14);
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:SELECT FLOOR(7 + (RAND() * 5));
http://dev.mysql.com/doc/refman/5.1/en/mathematical-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:
SELECT NOW() - INTERVAL FLOOR(RAND() * 14) DAY;
这篇关于在MySQL中插入/更新随机日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!