Re: select where id=random()*something returns two results
От | Rod Taylor |
---|---|
Тема | Re: select where id=random()*something returns two results |
Дата | |
Msg-id | 1063934713.40733.23.camel@jester обсуждение исходный текст |
Ответ на | select where id=random()*something returns two results (Ulrich Meis <u.meis@gmx.de>) |
Ответы |
Re: select where id=random()*something returns two results
|
Список | pgsql-bugs |
> select * from quotes where id=3D1+round(random()* cast ((select max(id) > from quotes) as double precision)); > id | quote | > author=20=20=20=20=20=20 > -----+-----------------------------------------------------------+------ > ----------- > 187 | Vergib Deinen Feinden, aber vergiss niemals ihre Namen. | John > F. Kennedy > 377 | Die Wirklichkeit ist nicht so oder so, sondern so und so. | Harry > Mulisch > (2 rows) >=20 > I'm not really into databases, but this sounds wrong. Most of the time, > I actually get 0 results. Random is calculated per call (in this case per comparison). So, the value you compare against for 187 is not the same as 377. UPDATE table SET column =3D random(); will show the effect. If you wrap randon() in a subselect, it will cause it to be evaluated once: SELECT * from quotes where id =3D 1+round((SELECT random()) * cast(....). However, a much faster query for your purposes would be: SELECT * FROM quotes ORDER BY random() LIMIT 1;
В списке pgsql-bugs по дате отправления: