pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Steve Midgley <public@misuse.org>
To: pgsql-sql@postgresql.org
Subject: Selecting rows with "static" ordering
Date: Thu, 26 Apr 2007 17:01:11 -0700
Message-ID: <20070427001004.B5F5D9FBABA@postgresql.org> (raw)

Hello,

I have a strange problem (or one that I've never had before anyway). I 
am searching for a list of "id's" for a given table (these id values 
are generated at run-time and held statically in an application-local 
variable).

 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)

As-is, of course, the above query will return the 5 records in a 
semi-random (i.e. unpredictable/unreliable) order. And I don't want to 
just "order by id" - I want to "order by id(3,2,5,1,4)" (if you see 
what I mean)

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.

Any input or advice would be appreciated,

Steve





view thread (6+ messages)  latest in thread

Message-ID: <20070427001004.B5F5D9FBABA@postgresql.org>
Permalink:  ../20070427001004.B5F5D9FBABA@postgresql.org/
Also on:    postgresql.org/message-id/20070427001004.B5F5D9FBABA@postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: public@misuse.org
  Subject: Re: Selecting rows with "static" ordering
  In-Reply-To: <20070427001004.B5F5D9FBABA@postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox