random database select

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
m2babaey
Forum Contributor
Posts: 364
Joined: Sun May 20, 2007 9:26 am

random database select

Post by m2babaey »

Hi
I want to display random articles in my homepage from "article" table in my database
how can I do that?
thanks
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Code: Select all

SELECT `content` FROM `table` ORDER BY RAND() LIMIT 0, 1;
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Choose the Right Board

Post by feyd »

[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:1. Select the correct board for your query. Take some time to read the guidelines in the sticky topic.
m2babaey
Forum Contributor
Posts: 364
Joined: Sun May 20, 2007 9:26 am

Post by m2babaey »

superdezign wrote:

Code: Select all

SELECT `content` FROM `table` ORDER BY RAND() LIMIT 0, 1;
thanks:P
what should be instead of "content"?
and what's the job for limit 0,1?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

m2babaey wrote:
superdezign wrote:

Code: Select all

SELECT `content` FROM `table` ORDER BY RAND() LIMIT 0, 1;
thanks:P
what should be instead of "content"?
and what's the job for limit 0,1?
You'll learn a lot more a lot faster if you learn to look for an answer in the MySQL manual first. We could tell you the specifics, but reading the manual will give you much more information.
m2babaey
Forum Contributor
Posts: 364
Joined: Sun May 20, 2007 9:26 am

Post by m2babaey »

Yes you are right
I was looking through net and books, but did not find something suitable in these 2 days
thanks,
m2babaey
Forum Contributor
Posts: 364
Joined: Sun May 20, 2007 9:26 am

Post by m2babaey »

Now what if i want to display random "article of the day"?
The above code will output a different articles each time the homepage is loaded. but what if I want to have only 1 article displayed in a single day?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Either come up with a randomization that uses the date to generate a unique value or use a separate table where you can associate a day with a randomly preselected record.
Post Reply