RAND() function in MySql
July 20, 2003 by daynah
Filed under Code Snippets
I was trying to use this query to get a random row from the database:
$myRandom = "SELECT * FROM MYTABLE ORDER BY RAND() LIMIT 1";
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:
Make: Electronics (Learning by Discovery)"This is teaching at its best!"
--Hans Camenzind, inventor of the 555 timer (the world's most successful integrated circuit), and author of... Read More >
Getting Started in ElectronicsThis is a complete electronics course in 128 pages! Author Forrest Mims teaches you the basics, takes you on a tour of analog and digital components, ... Read More >
Electronics and Circuit Analysis Study Guide: Signal Transforms, Fourier, Laplace & Z transform, Transfer function, Electronic components, Analog & Digital Circuits (Mobi Study Guides)Boost Your grades with this illustrated study guide! You will use it from an undergraduate school all the way to graduate school and beyond. Popularity: 19% [?]


@







