Getting A Random Row From A MySQL Table

26th December 2007

Getting a random row from a MySQL table requires the use of the RAND() function in the ORDER BY clause of the SELECT statement. This will generate a new random number for each row and order them by that new number. In order to get a single row this is combined with the LIMIT clause to limit the result to a single row.

SELECT * FROM theTable ORDER BY RAND() LIMIT 1;

Add new comment

The content of this field is kept private and will not be shown publicly.