Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id 86CBBBEAE59 for ; Wed, 25 Jul 2012 02:18:36 -0300 (ADT) Received: from [210.83.216.107] (helo=edgesoft.cn) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SttzV-0003HH-HK for pgsql-admin@postgresql.org; Wed, 25 Jul 2012 05:18:35 +0000 Received: from [10.18.27.77] (unknown [10.18.27.77]) (Authenticated sender: wangqi@edgesoft.cn) by mail.edgesoft.cn (Postfix) with ESMTPA id 5AB4D810F; Wed, 25 Jul 2012 13:18:12 +0800 (CST) Message-ID: <500F818F.1020106@edgesoft.cn> Date: Wed, 25 Jul 2012 13:18:07 +0800 From: wangqi User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:13.0) Gecko/20120614 Thunderbird/13.0.1 MIME-Version: 1.0 To: Tom Lane CC: pgsql-admin@postgresql.org Subject: Re: about select use random References: <500F5870.8010400@edgesoft.cn> <12603.1343184929@sss.pgh.pa.us> In-Reply-To: <12603.1343184929@sss.pgh.pa.us> Content-Type: text/plain; charset=GB2312 Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 210.83.216.107 (failed) X-Pg-Spam-Score: -1.1 (-) X-Archive-Number: 201207/74 X-Sequence-Number: 39791 Hi Tom If you don't want that, you can use a WITH to lock down the result of a single random() call. How should I do it? =D3=DA 2012-7-25 10:55, Tom Lane =D0=B4=B5=C0: > wangqi writes: >> =A1=F6SQL-3 >> select * from wk_mclid1 where id1=3D (round((random() * (10-1))::integ= er) >> + 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 > --=20 ---------------------------------------------------- Yours sincerely, Qi Wang Edgesoft (China) Co.,Ltd F26th Block B,Zhongli Building, No.32 Huoju Road, Hi-Tech Zone,Dalian,China Zip:116023 Tel:+86-411-84753511 Fax:+86-411-84753577 http://www.edgesoft.cn mailto:wangqi@edgesoft.cn ----------------------------------------------------