To randomly select a row from a MySql table, use the following:

SELECT * FROM tableName ORDER BY RAND() LIMIT 1

The RAND() function will randomly sort the selected data. The LIMIT X will select the first X data rows. In this example, X equals 1, so it is the equivalent to one row randomly selected. If you want to randomly select more than one row, set X to the number of random rows you want to retrieve.

This entry was posted on Tuesday, May 13th, 2008 at 7:02 am and is filed under MySQL. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply