Re: randomized order in select? - Mailing list pgsql-sql

From Bruno Wolff III
Subject Re: randomized order in select?
Date
Msg-id 20040310190250.GA6629@wolff.to
Whole thread Raw
In response to randomized order in select?  (Enver ALTIN <enver.altin@frontsite.com.tr>)
Responses Re: randomized order in select?
Re: randomized order in select?
List pgsql-sql
On Wed, Mar 10, 2004 at 18:48:17 +0200, Enver ALTIN <enver.altin@frontsite.com.tr> wrote:
> 
> collect an ID list, choose one randomly and retrieve it. I wish I could
> do something like:
> 
>     select tiptext from tips order by random limit 1
> 
> in PostgreSQL.

You can but it won't be very efficient (for large tables) as it will generate
a random ordering for the whole table, probably do a sort and then return the
first record.  The only thing different you need to do is add () after random:
select tiptext from tips order by random() limit 1

If the number of tips isn't very large doing the above is probably best.


pgsql-sql by date:

Previous
From: "scott.marlowe"
Date:
Subject: Re: randomized order in select?
Next
From: Robert Treat
Date:
Subject: Re: designer tool connect to PostgreSQL