[SOLVED] Selecting random "n" entries from a DB

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
zoki
Forum Newbie
Posts: 9
Joined: Tue Apr 23, 2002 3:09 pm
Location: Serbia
Contact:

[SOLVED] Selecting random "n" entries from a DB

Post by zoki »

OK for example, I have a query:

SELECT * FROM news WHERE cat='$cat' LIMIT 5;

The thing is, I want to select 5 random entries. Is that possible?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

try this

Code: Select all

SELECT * FROM news WHERE cat='$cat' ORDER BY rand() LIMIT 5;
User avatar
zoki
Forum Newbie
Posts: 9
Joined: Tue Apr 23, 2002 3:09 pm
Location: Serbia
Contact:

Post by zoki »

Yes, that worked. Thanks a lot.
Post Reply