DelphiFAQ Home Search:
General :: Databases :: mysql
General information about mysql - how to get around the differences between version 3.33 an 4, how to do stuff that you think you need a nested query for etc.

Articles:

This list is sorted by recent document popularity (not total page views).
New documents will first appear at the bottom.

Featured Article

How to pick a random record from a mysql table

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.

select word from wordlist ORDER BY RAND() LIMIT 1

Generated 0:00:40 on Jan 18, 2018