I was looking for a way to order the rows randomly in a MySQL query and the solution was not easy for me to come by. I am posting the solution here in hopes of helping others.
1 2 3 | SELECT * FROM my_table ORDER BY RAND() |
RAND() returns a random floating-point value but functions to randomly order the selection of rows in the above usage.
If you combine the query with LIMIT, you will end up with a random selection of rows from your table.
1 2 3 4 | SELECT * FROM my_table ORDER BY RAND() LIMIT 3 |
Assuming you have more than 3 rows in your table, you will always get 3 random rows from the query.
This type of function in MySQL is only valid if the number of records stored do not exceed about 10 thousand records. otherwise it is obsolete resource intensive on the server. (RAM and processor)
For cases is better to combine scalable enabling scripting language (eg php) + Mysql.
Greetings!