RAND() function in MySql

July 20, 2003 by  
Filed under Code Snippets

I was trying to use this query to get a random row from the database:


and I couldn’t figure out why it would continually get the previous number instead of some random number.

Ahh, but look what’s at the bottom of the page. ;)

“As of MySQL 3.23.52, MySQL changed the way RAND() functions, such that you MUST supply a SEED to get an actual random number. If you do not, each new connection will return close to the same number as the previous new connection. An example to test this would be to run the follow command multiple times in a row. mysql -u username -p -e’select rand()’ A basic way to seed this would be to run RAND(NOW()).”

So now I know, I must provide a random seed. ;)

Source: http://www.mysql.com

Related Products:

AmazonBasics 60-Inch Lightweight Tripod with BagAmazonBasics 60-Inch Lightweight Tripod with BagDeluxe lightweight tripod from AmazonBasics.
Energizer AA Batteries, Double A Battery Max Alkaline (24 Count) E91BP-24Energizer AA Batteries, Double A Battery Max Alkaline (24 Count) E91BP-24Long-lasting power and innovation is what Energizer MAX is all about. Introducing our longest-lasting Energizer MAX battery yet. With our latest impro... Read More >
Intel BX80684I78700K 8th Gen Core i7-8700K ProcessorIntel BX80684I78700K 8th Gen Core i7-8700K ProcessorOutstanding gaming experiences extend beyond personal gameplay to your entire gaming community. Share those experiences by live-streaming or recording, editing, and posting your epic highlights.
Related Posts Plugin for WordPress, Blogger...