Re: randomized order in select?

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: Enver ALTIN <enver(dot)altin(at)frontsite(dot)com(dot)tr>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: randomized order in select?
Date: 2004-03-10 19:02:50
Message-ID: 20040310190250.GA6629@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Wed, Mar 10, 2004 at 18:48:17 +0200,
Enver ALTIN <enver(dot)altin(at)frontsite(dot)com(dot)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.

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message scott.marlowe 2004-03-10 19:06:04 Re: randomized order in select?
Previous Message Jander 2004-03-10 18:23:10 Break a report in Run Time