By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,069 Members | 1,716 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,069 IT Pros & Developers. It's quick & easy.

randomly select records

P: n/a
I've a site where companies add their article.

I'de like to provide a "lasts articles" table. By this, I'll show last
articles inserted. But I won't always the same articles at any refresh.
Question 1: how to get a "random" selection from the database, giving more
priority to the last inserted (the ones with higher articleID)
Question 2: I'd like to provide one article by client. I won't show 3
articles from the same client only because he has been the last one to
insert his articles.

So I must randomly select by choosing between last articleID, but only one
per clientID.

simplified table is:

articleID
clientID
details
price.

It is possible with MySQL ? or any idea how to achieve this ?

PS:I've no access to mysql NG.

BOB

Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Bob Bedford wrote:

Hi Bob,
I've a site where companies add their article.

I'de like to provide a "lasts articles" table. By this, I'll show last
articles inserted. But I won't always the same articles at any refresh.
Question 1: how to get a "random" selection from the database, giving more
priority to the last inserted (the ones with higher articleID)
Do that in PHP and not in SQL.
I don't know if it is possible in SQL, but it sounds like a monsterquery to
me.

How to approach?
If you have all your articleid present in the database CONTINIOUSLY (no gaps
caused by deleting records), I think you can relatively conjure up some
algoritm that will produce one of the articleid's.

The excact formula depends on the distribution you want, of course.
How much bigger must the chance be for recent articles?

If you have gaps between the articleid's, I suggest you first get them in
ORDER BY articleid, and add indexnumbers to them in some array, like
1 articleid = 2
2 articleid = 3
3 articleid = 5
4 articleid = 9
etc

Then use the indexnumbers for the distribution.

(If you have trouble conjuring up some formula, ask here again)
Question 2: I'd like to provide one article by client. I won't show 3
articles from the same client only because he has been the last one to
insert his articles.

So I must randomly select by choosing between last articleID, but only one
per clientID.
I would also do this in PHP and not in SQL.
use ORDER BY clientid, articleid in your query, and loop over the result,
picking a random articleid PER clientid

simplified table is:

articleID
clientID
details
price.

It is possible with MySQL ? or any idea how to achieve this ?

PS:I've no access to mysql NG.
EVERYBODY has access to mySQL.
You can download it right now.
:-)

BOB


Regards,
Erwin Moller
Jul 17 '05 #2

P: n/a

"Erwin Moller"
<si******************************************@spam yourself.com> a écrit dans
le message de news: 42*********************@news.xs4all.nl...
Bob Bedford wrote:

Hi Bob,
I've a site where companies add their article.

I'de like to provide a "lasts articles" table. By this, I'll show last
articles inserted. But I won't always the same articles at any refresh.
Question 1: how to get a "random" selection from the database, giving
more
priority to the last inserted (the ones with higher articleID)


Do that in PHP and not in SQL.
I don't know if it is possible in SQL, but it sounds like a monsterquery
to
me.

How to approach?
If you have all your articleid present in the database CONTINIOUSLY (no
gaps
caused by deleting records), I think you can relatively conjure up some
algoritm that will produce one of the articleid's.

The excact formula depends on the distribution you want, of course.
How much bigger must the chance be for recent articles?

If you have gaps between the articleid's, I suggest you first get them in
ORDER BY articleid, and add indexnumbers to them in some array, like
1 articleid = 2
2 articleid = 3
3 articleid = 5
4 articleid = 9
etc

Then use the indexnumbers for the distribution.

(If you have trouble conjuring up some formula, ask here again)
Question 2: I'd like to provide one article by client. I won't show 3
articles from the same client only because he has been the last one to
insert his articles.

So I must randomly select by choosing between last articleID, but only
one
per clientID.


I would also do this in PHP and not in SQL.
use ORDER BY clientid, articleid in your query, and loop over the result,
picking a random articleid PER clientid

simplified table is:

articleID
clientID
details
price.

It is possible with MySQL ? or any idea how to achieve this ?

PS:I've no access to mysql NG.


EVERYBODY has access to mySQL.
You can download it right now.
:-)

Hi Erwin,

Thanks for suggestions.
I have a huge database, quering like you say will have a huge load on the
server.
I've seen MySQL allow to use RANDOM and subquery.
I think I must mix those things for doing that. The purpose of doing that is
to avoid to return 1000 results then thread only 5 of them. If the server
may returns directly the 5 items, it would just be great, and I'm sure it's
possible.

select distinct ClientID from client order by reverse(RAND()) LIMIT 0,5)
//get 5 different clients
Then for each client:

select distinct articleID from article where article.ClientID =
client.ClientID order by reverse(RAND()) LIMIT 0,1

This should work even if I haven't tested yet. The problem is that it
doesn't really select the last articles, it just select any random article
from any random client. I've a field called lastdatetime in the article
table. It does log the last modification datetime. I'd like to use this
value to get the best result, but actually don't know how.

Bob
Jul 17 '05 #3

P: n/a
>> PS:I've no access to mysql NG.

EVERYBODY has access to mySQL.
You can download it right now.


MYSQL NEWSGROUP (NG).

Also missing something before.

The main purpose is to track the preferred article the user is looking for.
Then provide a more appropriate offer for his need.
Takes this example:
For a computer hardware store.
At the first visit, I provide randomly article from the last entered, from
the various providers.
Then I see that the user is looking for a flat screen (by the search
engine): Here I show a more appropriate choice of randomly selected
articles, not getting them only randomly, but selecting them between flat
screens only. That's the purpose of what I'm looking for.

Bob
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.