randomly select records

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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:


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

PS:I've no access to mysql NG.


Re: randomly select records

Bob Bedford wrote:

Hi Bob,

Quoted text here. Click to load it

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

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

Then use the indexnumbers for the distribution.

(If you have trouble conjuring up some formula, ask here again)

Quoted text here. Click to load it

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

Quoted text here. Click to load it

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

Quoted text here. Click to load it

Erwin Moller

Re: randomly select records

"Erwin Moller"
Quoted text here. Click to load it
Hi Erwin,

Thanks for suggestions.
I have a huge database, quering like you say will have a huge load on the
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

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.


Re: randomly select records

Quoted text here. Click to load it


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.


Site Timeline