Re: Random tuple? - Mailing list pgsql-novice

From Josh Berkus
Subject Re: Random tuple?
Date
Msg-id 200210171613.42600.josh@agliodbs.com
Whole thread Raw
In response to Random tuple?  ("Brendan LeFebvre" <brendanl@iname.com>)
Responses Re: Random tuple?
List pgsql-novice
Brendan,

> Is there available a routine, intrinsic or otherwise, that allows for simple
> extraction of one(1) random tuple from a given table or query-result?
>
> (The specific application I had in mind was drawing a random product from a
> webstore's inventory to feature on a front or section web page).

SELECT tablea.*, random() as random_key
FROM tablea
ORDER BY random_key
LIMIT 1;

And you can modify the limit to select as many random rows as you want.

--
-Josh Berkus
 Aglio Database Solutions
 San Francisco


pgsql-novice by date:

Previous
From: Jon Jensen
Date:
Subject: Re: Random tuple?
Next
From: Philip Hallstrom
Date:
Subject: Re: Random tuple?