I’ve created a sample data set in a MySql database that I need to use to test generating some reports with. For one particular table I’ve got 100k records that were all created within a few hours of each other. Because of the way the sample data was generated I couldn’t set the datetime columns on the 100k records to the desired values.
How can I use an SQL statement to randomise the datetime columns on the 100k records so instead of being within a few hours of each other they are spread across a month? As an example I’d like to spread the records evenly across the interval 1st Oct 2012 to 31st Oct 2012.
We can use
RAND()function. Following MySQL Reference Manual, to obtain a random integerRin the rangei <= R < j, use the expressionFLOOR(i + RAND() * (j – i)). So for every row in your sample table, you’d need to run the following query: