MySQL: Update multiple rows with random values
You can do it with SQL only:
update rainfall
set time = concat(
lpad(floor(rand() * 24), 2, '0'),
':',
lpad(floor(rand() * 12) * 5, 2, '0')
);
I would use rand()
as follows:
update rainfall
set time = sec_to_time(floor(rand() * 60 * 60 * 24 / (5 * 60) * 5 * 60)
Expression rand() * 60 * 60 * 24
gives you a random number of seconds that represents a time. You can then use floor()
and multiplication to round it to the nearest 5 minutes. Finally, time_to_sec()
turns it to a time
.