How to Select Random Records in MySQL

The ability to select random records from a table in MySQL can be helpful. Luckily this is easy to do with the RAND() function.

RAND() returns a random floating point value between 0 and 1. You can select random records in MySQL by using the RAND() function together with ORDER and LIMIT clauses. Here is an example:

Since RAND() will return a random number, sorting the data by RAND() will shuffle the records that are output (values in the id column above are generally returned in ascending order). Using LIMIT will determine how many records to return in your query.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">