MySQL Random Row

by Carey on December 4, 2010

Post image for MySQL Random Row
  • Share
  • CevherShare
  • Share

To get a random row from your table, you can use the following trick:

SELECT * FROM myTable ORDER BY RAND() LIMIT 1

This will return a single random row from myTable.

Keep in mind you can also use a WHERE clause to limit the set you are choosing a random record from.

This is a more efficient way of finding a random result than it would be if you returned all the records from the table and looped through them in your code.

Leave a Comment

Previous post:

Next post: