Re: select a random record - Mailing list pgsql-general

From Tod McQuillin
Subject Re: select a random record
Date
Msg-id 20010901165829.L48344-100000@glass.pun-pun.prv
Whole thread Raw
In response to select a random record  (bj@zuto.de (Rainer Clasen))
List pgsql-general
On Sat, 1 Sep 2001, Rainer Clasen wrote:

> 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.

Sure, well, try this:

SELECT whatever FROM whereever WHERE whatever ORDER BY random() limit 5;

replace whatever and whereever with your selection criterie -- the order
by random() will randomise the results and limit 5 chooses only the first
5.
--
Tod McQuillin



pgsql-general by date:

Previous
From: Alvaro Herrera
Date:
Subject: Re: 2 tables, joins and same name...
Next
From: A_Schnabel@t-online.de (Andre Schnabel)
Date:
Subject: Re: select a random record