Re: Bug? Me or PostgreSQL.

From: Tod McQuillin <devin(at)spamcop(dot)net>
To: Christopher Sawtell <csawtell(at)xtra(dot)co(dot)nz>
Cc: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Bug? Me or PostgreSQL.
Date: 2001-02-19 05:47:14
Message-ID: Pine.GSO.4.31.0102182344180.13057-100000@sysadmin
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Ines.Klimann 2001-02-19 14:20:37 How to insert values with a new type ?
Previous Message Tom Lane 2001-02-19 05:37:01 Re: Bug? Me or PostgreSQL.