Get random results from a MySql table

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:

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.

Published by Cristian Radulescu

PHP // MySql // Linux

Leave a comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.