I never knew there was a built-in random function in MySQL. What i did was get all the rows and using PHP's array_rand to get a random set of rows. When using RAND() in MySQL together with the LIMIT keywords it returns only the number of rows required for a random. It really saved so many lines of my code.
We can get a random number from the MySQL server using:
mysql> SELECT RAND();
We can also add a seed value by adding an argument to our RAND function:
mysql> SELECT RAND(34);
To retrive rows in random we use:
mysql> SELECT * FROM tbl_name ORDER BY RAND();
Now with the LIMIT keyword added:
mysql> SELECT * FROM tbl_name ORDER BY RAND() LIMIT 1000;

Leave a comment