randomly select records

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob Bedford

    randomly select records

    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





  • Erwin Moller

    #2
    Re: randomly select records

    Bob Bedford wrote:

    Hi Bob,
    [color=blue]
    > 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)[/color]

    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)
    [color=blue]
    > 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.[/color]

    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
    [color=blue]
    >
    > 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.[/color]

    EVERYBODY has access to mySQL.
    You can download it right now.
    :-)
    [color=blue]
    >
    > BOB[/color]

    Regards,
    Erwin Moller

    Comment

    • Bob Bedford

      #3
      Re: randomly select records


      "Erwin Moller"
      <since_humans_r ead_this_I_am_s pammed_too_much @spamyourself.c om> a écrit dans
      le message de news: 4242dc41$0$154$ e4fe514c@news.x s4all.nl...[color=blue]
      > Bob Bedford wrote:
      >
      > Hi Bob,
      >[color=green]
      >> 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)[/color]
      >
      > 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)
      >[color=green]
      >> 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.[/color]
      >
      > 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
      >[color=green]
      >>
      >> 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.[/color]
      >
      > EVERYBODY has access to mySQL.
      > You can download it right now.
      > :-)
      >[/color]
      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.ClientI D =
      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


      Comment

      • Bob Bedford

        #4
        Re: randomly select records

        >> PS:I've no access to mysql NG.[color=blue]
        >
        > EVERYBODY has access to mySQL.
        > You can download it right now.[/color]

        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


        Comment

        Working...