How to pull random record 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
Crashin
Forum Contributor
Posts: 223
Joined: Mon May 06, 2002 3:42 pm
Location: Colorado

How to pull random record from a DB?

Post by Crashin »

Hey all...

I'd like to construct a query that will pull a random record from my DB (MySQL) for display on our homepage. Is there a quick and dirty way to do this?
kcomer
Forum Contributor
Posts: 108
Joined: Tue Aug 27, 2002 8:50 am

Post by kcomer »

Code: Select all

SELECT * FROM table_name ORDER BY RAND() limit 1;
Should work. Got it from the MySQL website. Imagine that.

Keith
User avatar
Crashin
Forum Contributor
Posts: 223
Joined: Mon May 06, 2002 3:42 pm
Location: Colorado

Post by Crashin »

Aw, shucks! MySQL has their own website??? Why, I never! :oops: Thanks, man! I'll do better research next time...
kcomer
Forum Contributor
Posts: 108
Joined: Tue Aug 27, 2002 8:50 am

Post by kcomer »

Just giving you a hard time. Mine will come sometime I'm sure.
Post Reply