Username / Password :   
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.

HumanX


"Your Linux News and Resource Site"
©2003 LinuxDig.Com. LinuxDig.Com, LinuxDig and Penguin Initiative are copyrights of LinuxDig.com.
Site Hosted By Digital Environments, Inc. This Website was Created with DE-Web Version 1.9.7.4,
The Fast, Web Based - Website Design Tool, Groupware and Web Hosting System by Digital Environments, Inc.
Groupware:Project Management, Sales Tracking, Web Site Design and News / Blogger all in one package.