X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id E17F3D1B534 for ; Wed, 10 Mar 2004 19:00:32 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 05935-05 for ; Wed, 10 Mar 2004 15:00:21 -0400 (AST) Received: from wolff.to (wolff.to [66.93.249.74]) by svr1.postgresql.org (Postfix) with SMTP id 322FBD1BB52 for ; Wed, 10 Mar 2004 15:00:19 -0400 (AST) Received: (qmail 6749 invoked by uid 500); 10 Mar 2004 19:02:50 -0000 Date: Wed, 10 Mar 2004 13:02:50 -0600 From: Bruno Wolff III To: Enver ALTIN Cc: pgsql-sql@postgresql.org Subject: Re: randomized order in select? Message-ID: <20040310190250.GA6629@wolff.to> Mail-Followup-To: Enver ALTIN , pgsql-sql@postgresql.org References: <1078937297.29628.12.camel@enver.office.frontsite.com.tr> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1078937297.29628.12.camel@enver.office.frontsite.com.tr> User-Agent: Mutt/1.5.6i X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200403/112 X-Sequence-Number: 17211 On Wed, Mar 10, 2004 at 18:48:17 +0200, Enver ALTIN wrote: > > collect an ID list, choose one randomly and retrieve it. I wish I could > do something like: > > select tiptext from tips order by random limit 1 > > in PostgreSQL. You can but it won't be very efficient (for large tables) as it will generate a random ordering for the whole table, probably do a sort and then return the first record. The only thing different you need to do is add () after random: select tiptext from tips order by random() limit 1 If the number of tips isn't very large doing the above is probably best.