how to pick 5 random rows

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

how to pick 5 random rows

Post by itsmani1 »

I need to pick 5 rows randomly form the database
how can i do so?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

When will you start to look these things up yourself? I'm pretty sure the order by rand has been mentionned various times overhere... And the limit clause (or whatever it's called for your sql dbms) even more...

even "sql get 5 random rows" returns useful results in a search-engine.
http://www.petefreitag.com/item/466.cfm
User avatar
phpdevuk
Forum Contributor
Posts: 220
Joined: Mon Jul 04, 2005 5:31 am
Location: UK
Contact:

Post by phpdevuk »

This sort of thing will do what you want.
easily found by googling tho ;)

Code: Select all

SELECT *							
FROM table
ORDER BY RAND()
LIMIT 0,5
Post Reply