Re: select a random record

From: Tod McQuillin <devin(at)spamcop(dot)net>
To: Rainer Clasen <bj(at)zuto(dot)de>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: select a random record
Date: 2001-09-01 08:00:12
Message-ID: 20010901165829.L48344-100000@glass.pun-pun.prv
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Alvaro Herrera 2001-09-01 08:46:30 Re: 2 tables, joins and same name...
Previous Message Rainer Clasen 2001-09-01 07:29:43 Re: select a random record