Re: select a random record

From: A_Schnabel(at)t-online(dot)de (Andre Schnabel)
To: <bj(at)zuto(dot)de>, <pgsql-general(at)postgresql(dot)org>
Subject: Re: select a random record
Date: 2001-09-01 07:07:04
Message-ID: 00a501c132b4$b5052580$0201a8c0@aschnabel.homeip.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

----- Original Message -----
From: "Rainer Clasen" <bj(at)zuto(dot)de>
Subject: [GENERAL] select a random record

...
>
> This jukebox should offer a random play mode. It should take into account
> when the title was played the last time. My Idea is to submit the query
> returning the records ordered by their last time of play, ignore a random
> number of records, fetch one record and drop the result set.
>
> I'm wondering wether there is a more elegant way to fetch a random record
> from a result.
>
> Rainer

The following select should do it:
SELECT title_name FROM titles
ORDER BY last_played DESC
LIMIT 1
OFFSET ( floor ( random () * 20);

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Rainer Clasen 2001-09-01 07:29:43 Re: select a random record
Previous Message Rainer Clasen 2001-09-01 06:57:50 select a random record