From: | Daniel Lundin <daniel(at)helena-daniel(dot)se> |
---|---|
To: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: selecting random row |
Date: | 2002-04-02 07:50:31 |
Message-ID: | 20020402075030.GA4022@shire |
Views: | Whole Thread | Raw Message | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
On tis, apr 02, 2002 at 06:57:21 +0000, Heiko Klein wrote:
> Hi,
>
> out of a table i.e. people:
> id last first sex age address
>
select id, random()
from people
order by 2
limit 1
;
The problem is that the entire table has to be sorted for each select. If it's
not very big, is suppose it's OK and better than your solution below.
> I would like to select a random id. Id's are a sequence, but some
> peoble have been deleted, so there are several id-holes in the
> table.
>
> Furthermore, I would like to specify the random person, like
>
> select 'random person id' from table where age > 60 and sex = 'm';
>
>
>
> Right now (in perl), I select the complete list of id's into a @list, do
> a $id = $list[rand(@list)] and have the right row. But this seems to be
> very time-consuming, and I would like to have it done completely in the
> Pg-database.
>
> Regards,
>
> Heiko
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to majordomo(at)postgresql(dot)org
>
From | Date | Subject | |
---|---|---|---|
Next Message | Holger Klawitter | 2002-04-02 08:12:02 | Re: selecting random row |
Previous Message | Martijn van Oosterhout | 2002-04-02 07:42:55 | Re: selecting random row |