Re: select where id=random()*something returns two results - Mailing list pgsql-bugs

From Kris Jurka
Subject Re: select where id=random()*something returns two results
Date
Msg-id Pine.LNX.4.33.0309182114020.26639-100000@leary.csoft.net
Whole thread Raw
In response to select where id=random()*something returns two results  (Ulrich Meis <u.meis@gmx.de>)
List pgsql-bugs
On Fri, 19 Sep 2003, Ulrich Meis wrote:

> select * from quotes where id=1+round(random()* cast ((select max(id)
> from quotes) as double precision));
>  id  |                           quote                           |
> author
> -----+-----------------------------------------------------------+------
> -----------
>  187 | Vergib Deinen Feinden, aber vergiss niemals ihre Namen.   | John
> F. Kennedy
>  377 | Die Wirklichkeit ist nicht so oder so, sondern so und so. | Harry
> Mulisch
> (2 rows)
>
> I'm not really into databases, but this sounds wrong. Most of the time,
> I actually get 0 results.

The problem is that random() is evaluated on each line giving a different
result.

Kris Jurka

pgsql-bugs by date:

Previous
From: Ulrich Meis
Date:
Subject: select where id=random()*something returns two results
Next
From: Rod Taylor
Date:
Subject: Re: select where id=random()*something returns two results