MySQL Forums
Forum List  »  Newbie

What's the best way to select a random row?
Posted by: Steve Cygan
Date: March 16, 2005 03:53PM

The query I'm using right now is:
SELECT * FROM table ORDER BY RAND() LIMIT 1

However, that isn't very random, especially when rows have been deleted from the table. I've seen suggestions to generate the random number within the program and then pass it to mysql's RAND function like this:
SELECT * FROM table ORDER BY RAND($random) LIMIT 1

I've also heard that the query itself is cpu intensive and a better way to do it would be like this:
SELECT *, RAND() AS r FROM table ORDER BY r LIMIT 1

Any suggestions?

Options: ReplyQuote


Subject
Written By
Posted
What's the best way to select a random row?
March 16, 2005 03:53PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.