Wednesday, January 16, 2008

Select a random record from a DB table

In Rails:
Post.find(:first, :order => "rand()")

In SQL:
SELECT * FROM posts ORDER BY RAND() LIMIT 1;

No comments: