Thread: Order by rand()
View Single Post
Old 06-10-2008, 06:24 PM   #6 (permalink)
Wildhoney
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

I can only give you the theory of how I did it. This isn't because I don't want to share it, but it has too many dependencies. What I did was the following:
  1. Generate a random number using PHP. Select from the table where the ID is more or equal to;
  2. Add that selected ID to an array and execute the first query;
  3. When we loop again, follow the same process but append IN() to the end with the previously selected IDs;
  4. Keep doing this until we get the amount we require.

As this is selecting an ID, which should be the primary key, it is a much faster select than choosing a random line from the database.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote