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

about select use random

From: wangqi <wangqi(at)edgesoft(dot)cn>
To: pgsql-admin(at)postgresql(dot)org
Subject: about select use random
Date: 2012-07-25 02:22:40
Message-ID: 500F5870.8010400@edgesoft.cn (view raw or flat)
Thread:
Lists: pgsql-admin
Hi everyone

■SQL-1
CREATE TABLE wk_mclid1(
id1 integer ,
PRIMARY KEY(id1)
);

■SQL-2
INSERT INTO wk_mclid1(id1)
VALUES(1),(2),(3),(4),(5),(6),(7),(8),(9),(10)
GO

■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?

============================================================================================
select * from wk_mclid1 where id1= (round((random() * (10-1))::integer)
+ 1)::integer;

id1
------
9

============================================================================================
select * from wk_mclid1 where id1= (round((random() * (10-1))::integer)
+ 1)::integer;

id1
------
4
6
7
============================================================================================
select * from wk_mclid1 where id1= (round((random() * (10-1))::integer)
+ 1)::integer;

id1
------

0 record(s) selected [Fetch MetaData: 94/ms] [Fetch Data: 0/ms]
============================================================================================

thanks!


Responses

pgsql-admin by date

Next:From: Tom LaneDate: 2012-07-25 02:55:29
Subject: Re: about select use random
Previous:From: Jesper KroghDate: 2012-07-24 19:58:21
Subject: Tuning checkpoint_segments and checkpoint_timeout.

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