Re: Bug? Me or PostgreSQL. - Mailing list pgsql-sql

From Tod McQuillin
Subject Re: Bug? Me or PostgreSQL.
Date
Msg-id Pine.GSO.4.31.0102182344180.13057-100000@sysadmin
Whole thread Raw
In response to Bug? Me or PostgreSQL.  (Christopher Sawtell <csawtell@xtra.co.nz>)
List pgsql-sql
On Mon, 19 Feb 2001, Christopher Sawtell wrote:

> How can this happen?
>
> chris=# select name from boys_names where
> boys_names.number=(random()*225+1)::int4;
>   name
> --------
>  Fred
>  Gunnar
>  Manuel
>  Rainer
> (4 rows)

It's not clear what you expected to see -- I'll guess you expected only
one row returned.

But random() is being called once for every row in the table -- it's not
surprising (if you have a lot of rows) that it comes up with a match more
than once.  Sometimes it might even return no rows at all.

SQL is not the best language for picking random members of a set.  You're
better off picking a random number in a procedural language and passing
that number as a constant to an embedded SQL query.
-- 
Tod McQuillin




pgsql-sql by date:

Previous
From: Tom Lane
Date:
Subject: Re: Bug? Me or PostgreSQL.
Next
From: Ines.Klimann@liafa.jussieu.fr
Date:
Subject: How to insert values with a new type ?