Plain and simple: how can I select, say, 4 or 5 random entries from a table? Does MySQL have a built in function for something like this?
| SitePoint Sponsor |





Plain and simple: how can I select, say, 4 or 5 random entries from a table? Does MySQL have a built in function for something like this?





depends on what version of mySQL you have. 3.23 has a built in RAND() function.
from mysql.com (http://www.mysql.com/doc/M/a/Mathema...functions.html):
RAND()
RAND(N)
Returns a random floating-point value in the range 0 to 1.0. If an integer argument N is specified, it is used as the seed value:
mysql> select RAND();
-> 0.5925
mysql> select RAND(20);
-> 0.1811
mysql> select RAND(20);
-> 0.1811
mysql> select RAND();
-> 0.2079
mysql> select RAND();
-> 0.7888
You can't use a column with RAND() values in an ORDER BY clause, because ORDER BY would evaluate the column multiple times. In MySQL Version 3.23, you can, however, do: SELECT * FROM table_name ORDER BY RAND() This is useful to get a random sample of a set SELECT * FROM table1,table2 WHERE a=b AND c<d ORDER BY RAND() LIMIT 1000. Note that a RAND() in a WHERE clause will be re-evaluated every time the WHERE is executed.
Last edited by creole; May 24, 2001 at 07:18.
Adobe Certified Coldfusion MX 7 Developer
Adobe Certified Advanced Coldfusion MX Developer
My Blog (new) | My Family | My Freelance | My Recipes





Thanks creole -- I had a vauge memory of that. I appreciate it!![]()





hehe...
that gonna be the ONLY time I eve answer a question that you would ask about programming/database.
Adobe Certified Coldfusion MX 7 Developer
Adobe Certified Advanced Coldfusion MX Developer
My Blog (new) | My Family | My Freelance | My Recipes
Bookmarks