Search Postgresql Archives

Re: Fwd: Unexpected Multiple Records from Randomized Query

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



=?UTF-8?B?6rmA66qF7KSA?= <audwns525@xxxxxxxxx> writes:
> explain analyze
> SELECT * FROM users WHERE name = 'User '||trunc(random()*100) ;

> I expected the result to return one record. However, in some cases, the
> result comes back with 2 or 3 records. What am I doing wrong?

random() is re-evaluated at each row, so it's not that surprising
if you sometimes get multiple matches.  This is the same behavior
that you relied on to fill the table with not-all-the-same names.

The preferred way to avoid that is to stuff the random() call into
a CTE:

WITH x AS (SELECT random() AS r)
SELECT * FROM users, x WHERE name = 'User '||trunc(r*100) ;

or in this case better to shove the whole constant computation
into the CTE.

			regards, tom lane





[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]

  Powered by Linux