Re: Problem with ORDER BY and random() ?

From: Mike Mascari <mascarm(at)mascari(dot)com>
To: Jean-Francois(dot)Doyon(at)CCRS(dot)NRCan(dot)gc(dot)ca
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Problem with ORDER BY and random() ?
Date: 2003-09-23 22:35:04
Message-ID: 3F70CA98.2010809@mascari.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Jean-Francois(dot)Doyon(at)CCRS(dot)NRCan(dot)gc(dot)ca wrote:

> Hello,
>
> I'm trying to retrieve a limited number of random rows, and order them by a
> column, and am not having any luck with that last part:
>
> SELECT * FROM tablename ORDER BY random(), id LIMIT 10

How about:

SELECT * FROM
(SELECT * FROM tablename ORDER BY random() LIMIT 10) AS data
ORDER BY id;

HTH,

Mike Mascari
mascarm(at)mascari(dot)com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Jean-Francois.Doyon 2003-09-23 22:41:48 Re: Problem with ORDER BY and random() ?
Previous Message Stephan Szabo 2003-09-23 22:31:34 Re: Problem with ORDER BY and random() ?