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

Re: about select use random

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: wangqi <wangqi(at)edgesoft(dot)cn>
Cc: pgsql-admin(at)postgresql(dot)org
Subject: Re: about select use random
Date: 2012-07-25 02:55:29
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-admin
wangqi <wangqi(at)edgesoft(dot)cn> writes:
> SQL-3
> select * from wk_mclid1 where id1= (round((random() * (10-1))::integer)
> + 1)::integer;

> When we excute SQL-3,sometimes the result was 0 record or more than 1
> record.

> Why?

random() is re-evaluated for each row scanned by the SELECT.

If you don't want that, you can use a WITH to lock down the result of a
single random() call.

			regards, tom lane

In response to


pgsql-admin by date

Next:From: wangqiDate: 2012-07-25 03:33:02
Subject: Re: about select use random
Previous:From: wangqiDate: 2012-07-25 02:22:40
Subject: about select use random

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