Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id DFE6B19E3CC1 for ; Wed, 25 Jul 2012 00:33:32 -0300 (ADT) Received: from [210.83.216.107] (helo=edgesoft.cn) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1StsLp-0001gk-5x for pgsql-admin@postgresql.org; Wed, 25 Jul 2012 03:33:31 +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 41D8A810F; Wed, 25 Jul 2012 11:33:06 +0800 (CST) Message-ID: <500F68EE.5000602@edgesoft.cn> Date: Wed, 25 Jul 2012 11:33:02 +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.6 (+) X-Archive-Number: 201207/73 X-Sequence-Number: 39790 Thanks Tom, I try 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 ----------------------------------------------------