Skip site navigation (1) Skip section navigation (2)

Re: select where id=random()*something returns two results

From: Kris Jurka <books(at)ejurka(dot)com>
To: Ulrich Meis <u(dot)meis(at)gmx(dot)de>
Cc: <pgsql-bugs(at)postgresql(dot)org>
Subject: Re: select where id=random()*something returns two results
Date: 2003-09-19 01:15:21
Message-ID: Pine.LNX.4.33.0309182114020.26639-100000@leary.csoft.net (view raw or flat)
Thread:
Lists: 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


In response to

pgsql-bugs by date

Next:From: Rod TaylorDate: 2003-09-19 01:25:14
Subject: Re: select where id=random()*something returns two results
Previous:From: Ulrich MeisDate: 2003-09-19 00:49:18
Subject: select where id=random()*something returns two results

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group