Skip to main content
July 2, 2011
Answered

Is it possible to randomly output query

  • July 2, 2011
  • 1 reply
  • 8003 views

It is a website with about 400 affiliates, on one page people can see recent updates. Certain affiliates modify on purpose often small things on their website in order to end up high on this list. therefore we find always the same couple of affiliates on top of the list. Is it possible to randomly output the query that gets the most recent updates to give everybody equal chances?

I realise that in the query there is a ORDER BY clause that orders by most recent date. What I need is the 40 most recent updates and output them randomly.

<cfquery name="lastupdates" datasource="#request.dbc#">
        SELECT
            handelaaruuid
            , projectID, naam
            , websiteurl
            , gemeente
            , animatie
            , type_adverteerder
            , categoryid
            , subcatid,
             datum_voorlaatste_aanpassing AS datum
             , CONVERT(nvarchar, datum_voorlaatste_aanpassing, 106) AS datum2
        FROM      LastUpdates
        WHERE     (projectID = 1)
        ORDER BY datum desc
    </cfquery>

    This topic has been closed for replies.
    Correct answer Adam Cameron.

    I just googled "sql order by random":

    http://lmgtfy.com/?q=sql+order+by+random

    There seems to be a bunch of stuff there that answers your question...

    PS: if you're asking DB-specific questions, it's important that you tell us what DB system you're running, as the answer will almost always be different from system to system.  there's some good tips on how to post here: http://www.catb.org/~esr/faqs/smart-questions.html

    --

    Adam

    1 reply

    Adam Cameron.Correct answer
    Inspiring
    July 2, 2011

    I just googled "sql order by random":

    http://lmgtfy.com/?q=sql+order+by+random

    There seems to be a bunch of stuff there that answers your question...

    PS: if you're asking DB-specific questions, it's important that you tell us what DB system you're running, as the answer will almost always be different from system to system.  there's some good tips on how to post here: http://www.catb.org/~esr/faqs/smart-questions.html

    --

    Adam

    July 2, 2011

    Hi Adam,

    Thanks for you very quick reply. In fact i was only focussed on random output and couldn't find anything on Google that made sense. i didn't think about the statement you used to google. This should be of great help.

    Bianca

    Inspiring
    July 2, 2011

    Fair enough.

    As a rule of thumb, I think it's best to do the data manipulation on the DB, and leave CF to do the presentation of the data.  I guess in this case it's arguable whether this is data manipulation or data presentation.  However if the DB can fulfil the data-manipulation requirement... I'll do it on the DB.

    Equally, sometimes it's not until one steps back and talks to someone else about an issue that a different approach presents itself.

    --
    Adam