How to pick a random record from a mysql table
1 comments. Current rating: (1 votes). Leave comments and/ or rate it.
Question:I have a huge table in mysql with suitable passwords and want to pick a word from this table. If I use a simple select statement, I always get the first word.
How can I pick a random record from a mysql table (or result set, in general)?
Answer:Use the order by RAND() phrase, and for performance reasons, combine it with LIMIT 1 as shown in the example below.
There was a bug in older mysql versions (prior to version 4) where this RAND() phrase did not really lead to truely random results but always to the same rows after a connection was started.
This has been resolved with version 4.