Re: select 2 random rows

From: Josh Berkus <josh(at)agliodbs(dot)com>
To: Rory Campbell-Lange <rory(at)campbell-lange(dot)net>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: select 2 random rows
Date: 2002-06-27 22:58:00
Message-ID: 200206271558.00392.josh@agliodbs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Rory,

> I'd like to select 2 random rows from a table.
> At the moment I'm returning a couple of hundred rows into a perl hash
> and randomising it there, but it would be great if I could do it in the
> database call.
>
> Each row has an id number created using the SERIAL type.

This is actually quite easy:

SELECT *, RANDOM() as random_sort FROM the_table
ORDER BY random_sort LIMIT 2;

--
-Josh Berkus

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Josh Berkus 2002-06-27 23:01:08 Re: command length?
Previous Message stev knowles 2002-06-27 22:42:33 command length?