Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id BDD669FC13D for ; Fri, 27 Apr 2007 11:12:29 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 90231-07 for ; Fri, 27 Apr 2007 11:12:26 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from sipala.earlham.edu (sipala.earlham.edu [159.28.1.75]) by postgresql.org (Postfix) with ESMTP id 949319FC13C for ; Fri, 27 Apr 2007 11:12:26 -0300 (ADT) Received: from [159.28.7.5] (ec454.lly.earlham.edu [159.28.7.5]) (authenticated bits=0) by sipala.earlham.edu (8.13.6/8.13.6) with ESMTP id l3RECCEG004402 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NO); Fri, 27 Apr 2007 10:12:13 -0400 (EDT) X-Authentication-Warning: sipala.earlham.edu: Host ec454.lly.earlham.edu [159.28.7.5] claimed to be [159.28.7.5] In-Reply-To: <20070427001004.B5F5D9FBABA@postgresql.org> References: <20070427001004.B5F5D9FBABA@postgresql.org> Mime-Version: 1.0 (Apple Message framework v752.3) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: Cc: PostgreSQL SQL List Content-Transfer-Encoding: 7bit From: Kevin Hunter Subject: Re: Selecting rows with "static" ordering Date: Fri, 27 Apr 2007 10:11:56 -0400 To: Steve Midgley X-Mailer: Apple Mail (2.752.3) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200704/275 X-Sequence-Number: 28275 At 8:01p -0400 on 26 Apr 2007, Steve Midgley wrote: > From that application, I want to retrieve all those rows, and I > want them in the order they are currently stored in that variable. > So take for example this foreign application variable: > > ids = "3,2,5,1,4" > > The application then executes this sql: > > select * from table where id in (3,2,5,1,4) > Is there a "neat trick" that anyone knows for pulling this off in a > single query? Basically right now I'm issuing 5 queries to the > backend to ensure ordering but this horribly inefficient. Why not do this particular ordering on the application side? With this example SQL, and assuming you've PRIMARY KEYed( id ), you're guaranteed to get a unique id for each tuple. Presumably, you've got the order you want stored within your application, so just pull it out of a hash in the order you need it. Or is it terribly more complicated than this? Kevin