LinuxDig.Com Technology Articles Your Linux News and Resource Site
LinuxDig.Com Linux News : Generating Random Results From MySql with RAND()
Author: HumanX | Tuesday April 13, 2004
A look into MySql's RAND function and producing random results from your queries.
Producing random results from your queries is needed for a variety of applications from games to contact generation. MySql makes this process quite simple with the RAND function. The RAND function can be used to create an integer from 0 to 1.0, but when used with ORDER BY (since 3.23), MySql grabs your data in random order.
Example: Non Random Query:
SELCT * FROM animals LIMIT 3;
--------
| bird |
| cat |
| dog |
--------
Example: Random Query:
SELCT * FROM animals ORDER BY RAND() LIMIT 3;
------------
| lion |
| elephant |
| penguin |
------------
The random function is a great way to create a quote display system on your web site or to display advertisements.