Best unofficial Apache Server developers community
Username
Forgot password?
Sign in with Twitter account
Sign in with Facebook account

MySQL: How to retrieve a random row or multiple random rows?

1

82 views

I have a MySQL database table that stores the URLs of photos. I need to pull 5 random records from the database of a particular type. I can pull 5 records like this:

SELECT Photos.* 
FROM Photos 
WHERE Photos.Type_ID = 4 
LIMIT 5

Now I need help trying to figure out how to pull different records every time. How can I retrieve random rows from this result set?

asked January 3, 2011 11:47 am CST
posted via StackOverflow

3 Answers

4
 

You can use ORDER BY RAND() to get random rows in your query.

answered January 3, 2011 12:23 pm CST
3
 
SELECT Photos.* 
FROM Photos 
ORDER BY RAND()
LIMIT 5

answered January 3, 2011 12:23 pm CST
0
 

Google points to this detailed page. Looks like it works. I am sure it can't ensure distinct record each time, but worth trying. http://akinas.com/pages/en/blog/mysql_random_row/

answered January 3, 2011 12:23 pm CST

Your answer

Join with account you already have


Sign in with Twitter account
Sign in with Facebook account
Sign in with Google Friend Connect

Preview
Similar questions