pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bruno Wolff III <bruno@wolff.to>
To: Enver ALTIN <enver.altin@frontsite.com.tr>
Cc: pgsql-sql@postgresql.org
Subject: Re: randomized order in select?
Date: Wed, 10 Mar 2004 13:02:50 -0600
Message-ID: <20040310190250.GA6629@wolff.to> (raw)
In-Reply-To: <1078937297.29628.12.camel@enver.office.frontsite.com.tr>
References: <1078937297.29628.12.camel@enver.office.frontsite.com.tr>

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.



view thread (7+ messages)  latest in thread

Message-ID: <20040310190250.GA6629@wolff.to>
Permalink:  ../20040310190250.GA6629@wolff.to/
Also on:    postgresql.org/message-id/20040310190250.GA6629@wolff.to

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: bruno@wolff.to, enver.altin@frontsite.com.tr
  Subject: Re: randomized order in select?
  In-Reply-To: <20040310190250.GA6629@wolff.to>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox