随机日期如下:针对于datetime格式
CREATE TABLE test_random_time (
id int,
newstime datetime
);
INSERT INTO test_random_time
SELECT 1, '2012-11-13 01:00:00' UNION ALL
SELECT 2, '2012-11-13 02:00:00' UNION ALL
SELECT 3, '2012-11-13 03:00:00' UNION ALL
SELECT 4, '2012-11-13 04:00:00' UNION ALL
SELECT 5, '2012-11-13 05:00:00';
-- 20点至23点。
-- 区间=3小时=180分钟=10800秒
-- 下面更新时间 = '2012-11-13 20:00:00' 之后的 随机秒数。(区间在 1- 10800 之间)
UPDATE test_random_time
SET
newstime = DATE_ADD('2012-11-13 20:00:00', INTERVAL FLOOR(1 + (RAND() * 10800)) SECOND )
WHERE
DATE(newstime) = '2012-11-13';
针对于timestamp:
UPDATE `表名` a SET a.列名= (SELECT 1419955200-(FLOOR(1 + (RAND() * 12))*2678400)-(FLOOR(1 + (RAND() * 31))*86400)-FLOOR(1 + (RAND() * 86400)))
插入随机数:
UPDATE 表名 a set a.列名=ceiling(rand()*500)