Re: about select use random - Mailing list pgsql-admin

From Tom Lane
Subject Re: about select use random
Date
Msg-id 12603.1343184929@sss.pgh.pa.us
Whole thread Raw
In response to about select use random  (wangqi <wangqi@edgesoft.cn>)
Responses Re: about select use random
Re: about select use random
List pgsql-admin
wangqi <wangqi@edgesoft.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

pgsql-admin by date:

Previous
From: wangqi
Date:
Subject: about select use random
Next
From: wangqi
Date:
Subject: Re: about select use random