<suvisor.r...@gmail.com> wrote:
 
> select * from testt where id = (random()* 100000)::integer;
> 
> And sometimes it comes out something like this:
> id | val
> -------+--------
> 11894 | 15051
> 29233 | 42198
> 80725 | 90213
> 85688 | 100992
> 88017 | 108075
> (5 rows)
> Here can be 2, 3 or other rows amount in result... But must be
> only one!
 
No, what you have written will scan the entire table and give each
row a 1 in 100000 chance of being selected.  Maybe something like
this would give you what you want:
 
select t.* from (select (random()* 100000)::integer) n(r)
  join testt t on (t.id = n.r);
 
By the way, you might want to tweak that random number before
casting it to int, or you might not get *any* rows back:
 
test=# select ('0.0000000001'::float * 100000)::int;
 int4 
------
    0
(1 row)
 
Maybe something like:
 
(select floor(random() * 100000)::int + 1)
 
-Kevin

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Reply via email to