Re: Selecting rows with "static" ordering

From: "Phillip Smith" <phillip(dot)smith(at)weatherbeeta(dot)com(dot)au>
To: "'Steve Midgley'" <public(at)misuse(dot)org>, <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Selecting rows with "static" ordering
Date: 2007-04-27 00:27:41
Message-ID: 005501c78862$de21d440$9b0014ac@wbaus090
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

The best I can think of off the top of my head would still be multiple SQL,
but at least it would be in one transaction block:

BEGIN;
SELECT '1' AS ordering, t1.* INTO TEMP TABLE work_table FROM t1 WHERE t1.id
= '3';
SELECT '2' AS ordering, t1.* INTO TEMP TABLE work_table FROM t1 WHERE t1.id
= '2';
SELECT '3' AS ordering, t1.* INTO TEMP TABLE work_table FROM t1 WHERE t1.id
= '5';
SELECT '4' AS ordering, t1.* INTO TEMP TABLE work_table FROM t1 WHERE t1.id
= '1';
SELECT '5' AS ordering, t1.* INTO TEMP TABLE work_table FROM t1 WHERE t1.id
= '4';
SELECT t1.* FROM work_table ORDER BY ordering;
COMMIT;

Something to that effect...? It's at least makes it only 1 connection from
the client to the database.

-----Original Message-----
From: pgsql-sql-owner(at)postgresql(dot)org [mailto:pgsql-sql-owner(at)postgresql(dot)org]
On Behalf Of Steve Midgley
Sent: Friday, 27 April 2007 10:01
To: pgsql-sql(at)postgresql(dot)org
Subject: [SQL] Selecting rows with "static" ordering

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

---------------------------(end of broadcast)---------------------------
TIP 7: You can help support the PostgreSQL project by donating at

http://www.postgresql.org/about/donate

*******************Confidentiality and Privilege Notice*******************

The material contained in this message is privileged and confidential to
the addressee. If you are not the addressee indicated in this message or
responsible for delivery of the message to such person, you may not copy
or deliver this message to anyone, and you should destroy it and kindly
notify the sender by reply email.

Information in this message that does not relate to the official business
of Weatherbeeta must be treated as neither given nor endorsed by Weatherbeeta.
Weatherbeeta, its employees, contractors or associates shall not be liable
for direct, indirect or consequential loss arising from transmission of this
message or any attachments

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message chester c young 2007-04-27 00:35:43 Re: Selecting rows with "static" ordering
Previous Message Steve Midgley 2007-04-27 00:01:11 Selecting rows with "static" ordering