pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tod McQuillin <devin@spamcop.net>
To: Christopher Sawtell <csawtell@xtra.co.nz>
Cc: pgsql-sql@postgresql.org
Subject: Re: Bug? Me or PostgreSQL.
Date: Sun, 18 Feb 2001 23:47:14 -0600 (CST)
Message-ID: <Pine.GSO.4.31.0102182344180.13057-100000@sysadmin> (raw)
In-Reply-To: <01021917450300.27125@berty>

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





view thread (3+ messages)

Message-ID: <Pine.GSO.4.31.0102182344180.13057-100000@sysadmin>
Permalink:  ../Pine.GSO.4.31.0102182344180.13057-100000@sysadmin/
Also on:    postgresql.org/message-id/Pine.GSO.4.31.0102182344180.13057-100000@sysadmin

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: devin@spamcop.net, csawtell@xtra.co.nz
  Subject: Re: Bug? Me or PostgreSQL.
  In-Reply-To: <Pine.GSO.4.31.0102182344180.13057-100000@sysadmin>
  References: <01021917450300.27125@berty> <Pine.GSO.4.31.0102182344180.13057-100000@sysadmin>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox