Skip to content
Advertisement

Set MySQL timestamp to random time within past 24 hours

I have a timestamp column in my MySQL table.

I’m wanting to set this timestamp to a random time within the past 24 hours for all rows in the table.

I know I can update all the rows doing this:

UPDATE table SET timestamp = 

But I can’t find if there’s a way to set a random timestamp that’s occurred within the past 24 hours so that each row has a different time.

Advertisement

Answer

You can use:

UPDATE table
    SET timestamp = now() - interval floor((24*60*60)*rand()) second;
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement