Home >Database >Mysql Tutorial >How to Insert Random Datetimes Within a Specific Range in MySQL?
Inserting Random Datetimes within a Range in MySQL
Inserting random datetime values within a given range can be challenging using SQL. Given a range such as "2010-04-30 14:53:27" to "2012-04-30 14:53:27," precisely inserting values within that range can be confusing.
Solution:
To insert random datetime values within a specified range, use the following approach:
INSERT INTO `sometable` VALUES( FROM_UNIXTIME( UNIX_TIMESTAMP('2010-04-30 14:53:27') + FLOOR(0 + (RAND() * 63072000)) ) )
Explanation:
While this approach is fairly accurate, it should be noted that over longer time periods, factors such as leap years may result in minor deviations from the exact range.
The above is the detailed content of How to Insert Random Datetimes Within a Specific Range in MySQL?. For more information, please follow other related articles on the PHP Chinese website!