Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Please explain this mysql query.

Tags:

mysql

SELECT * FROM dogs order by rand(dayofyear(CURRENT_DATE)) LIMIT 1

It seems to me that it orders a database by a random number, and this number changes every day. This is a guess, as it'll take me a day to find out if this is true!

How can I change this query to order a database by a new random number every minute rather than every day? I tried this:

SELECT * FROM dogs order by rand(minuteofhour(CURRENT_DATE)) LIMIT 1

but it didn't work :(

Thanks for your time!

like image 746
Starkers Avatar asked Jan 23 '26 16:01

Starkers


1 Answers

A random number generator (RNG) usually needs a 'seed value', a value that is used to generate random numbers. If the seed value is always the same, the sequence of random numbers is always the same. This explains why it changes every day.

The easiest way to solve your problem (change it to every minute) is to find a seed value that changes every minute. A good one would be ROUND(UNIX_TIMESTAMP()/60).

SELECT * FROM dogs order by rand(ROUND(UNIX_TIMESTAMP()/60)) LIMIT 1
like image 96
Tom van der Woerdt Avatar answered Jan 25 '26 07:01

Tom van der Woerdt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!