Get random results from a MySql table

Cristian Radulescu • August 20, 2011

It is possible to retrieve random results from a MySql table using the RAND() mathematical function.

In order to get the random results, the RAND() function should be used in the ORDER BY clause, like in the example below:

-- Get random results
SELECT * FROM `my_table` ORDER BY RAND();

Retrieving random results directly from MySql is useful since you don't need another step in your application in order to manually randomize the sql results. For example if the sql results are retrieved without being randomized you need to use a "shuffle" like function to get random results in PHP.