CoderZone.org

Category: >> mySQL >> Fast random row picker #1 Bookmark and Share

<< lastnext >>

Snippet Name: Fast random row picker #1

Description: This method of selecting a random row is fairly fast.

Comment: (none)

Author: CoderZone
Language: MYSQL
Highlight Mode: MYSQL
Last Modified: February 28th, 2009

LOCK TABLES foo READ; 
 
SELECT FLOOR(RAND() * COUNT(*)) AS rand_row FROM foo; 
 
SELECT * FROM foo LIMIT $rand_row, 1; 
 
UNLOCK TABLES; 
 
There haven't been any comments added for this snippet yet. You may add one if you like.  Add a comment 
© coderzone.org | users online: 18