From: Phillip Smith <phillip.smith@weatherbeeta.com.au>
To: 'Steve Midgley' <public@misuse.org>
To: pgsql-sql@postgresql.org
Subject: Re: Selecting rows with "static" ordering
Date: Fri, 27 Apr 2007 10:27:41 +1000
Message-ID: <005501c78862$de21d440$9b0014ac@wbaus090> (raw)
In-Reply-To: <20070427001004.B5F5D9FBABA@postgresql.org>
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@postgresql.org [mailto:pgsql-sql-owner@postgresql.org]
On Behalf Of Steve Midgley
Sent: Friday, 27 April 2007 10:01
To: pgsql-sql@postgresql.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
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: phillip.smith@weatherbeeta.com.au, public@misuse.org
Subject: Re: Selecting rows with "static" ordering
In-Reply-To: <005501c78862$de21d440$9b0014ac@wbaus090>
* 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