Re: Un enregistrement au hasard ?

From: Sébastien Dinot <sebastien(dot)dinot(at)free(dot)fr>
To: pgsql-fr-generale(at)postgresql(dot)org
Subject: Re: Un enregistrement au hasard ?
Date: 2008-11-12 22:28:27
Message-ID: 20081112222827.GA7009@dinot.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-fr-generale

Sébastien Dinot a écrit :
> Mea culpa, c'est moi qui t'ai « enduit d'erreur ». Désolé. (c:

Finalement, non, même pas. « ORDER BY RANDOM() » semble bien être la
convention pour obtenir dans PostgreSQL un tirage aléatoire.

A ce sujet, la solution est fournie dans la FAQ de PostgreSQL :

http://www.postgresql.org/docs/faqs.FAQ.html#item4.1

------------------------------------------------------------------------
4.1) How do I SELECT only the first few rows of a query? A random row?

To retrieve only a few rows, if you know at the number of rows needed at
the time of the SELECT use LIMIT . If an index matches the ORDER BY it
is possible the entire query does not have to be executed. If you don't
know the number of rows at SELECT time, use a cursor and FETCH.

To SELECT a random row, use:

SELECT col
FROM tab
ORDER BY random()
LIMIT 1;
------------------------------------------------------------------------

Sébastien

--
Sébastien Dinot, sebastien(dot)dinot(at)free(dot)fr
http://sebastien.dinot.free.fr/
Ne goûtez pas au logiciel libre, vous ne pourriez plus vous en passer !

In response to

Browse pgsql-fr-generale by date

  From Date Subject
Next Message Sébastien Dinot 2008-11-12 23:12:05 Re: Un enregistrement au hasard ?
Previous Message Sébastien Dinot 2008-11-12 22:16:02 Re: Un enregistrement au hasard ?