Skip site navigation (1) Skip section navigation (2)

Re: Random tuple?

From: Josh Berkus <josh(at)agliodbs(dot)com>
To: "Brendan LeFebvre" <brendanl(at)iname(dot)com>,"Pgsql-Novice(at)Postgresql(dot) Org" <pgsql-novice(at)postgresql(dot)org>
Subject: Re: Random tuple?
Date: 2002-10-17 23:13:42
Message-ID: 200210171613.42600.josh@agliodbs.com (view raw or flat)
Thread:
Lists: 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


In response to

Responses

pgsql-novice by date

Next:From: Josh BerkusDate: 2002-10-18 00:08:34
Subject: Re: Random tuple?
Previous:From: Randy NeumannDate: 2002-10-17 20:15:11
Subject: Re: char() or varchar() for frequently used column

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group