RedCoupe

Random rows in MySQL

by on Sep.08, 2010, under Web design / development

A competition has been running on a website and all the entries were stored in a MySQL table.

Had to get a random record to pick a winner once the closing date had passed.

Here’s the SQL I used:

SELECT *
FROM t_tablename
ORDER BY RAND()
LIMIT 1

If I had wanted to pick out 5 random winners, then change the LIMIT value to LIMIT 5.

:

Leave a Reply

You must be logged in to post a comment.

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!