Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id D68C69FBF3D for ; Thu, 26 Apr 2007 21:28:09 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 23490-01-4 for ; Thu, 26 Apr 2007 21:28:00 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from weatherbeeta.com.au (218-214-212-153.people.net.au [218.214.212.153]) by postgresql.org (Postfix) with SMTP id 594FE9FC01C for ; Thu, 26 Apr 2007 21:28:04 -0300 (ADT) Received: (qmail 20194 invoked from network); 27 Apr 2007 00:28:02 -0000 X-NetworkBox-HamSign: 0101;OUT;wbeetaau1;85fbc279ef6a12854cdc0f608a5aee04; Received: from mail.weatherbeeta.com.au (10.10.0.2) by network-box.weatherbeeta.com.au with SMTP; 27 Apr 2007 00:28:01 -0000 Received: (qmail 22531 invoked by uid 1166); 27 Apr 2007 10:27:56 +1000 Received: from 172.20.0.155 by mail.weatherbeeta.com.au (envelope-from , uid 1165) with qmail-scanner-1.25-st-qms (clamdscan: 0.88.6/2315. perlscan: 1.25-st-qms. Clear:RC:1(172.20.0.155):. Processed in 0.354503 secs); 27 Apr 2007 00:27:56 -0000 X-Antivirus-mail.weatherbeeta.com.au-Mail-From: phillip.smith@weatherbeeta.com.au via mail.weatherbeeta.com.au X-Antivirus-mail.weatherbeeta.com.au: 1.25-st-qms (Clear:RC:1(172.20.0.155):. Processed in 0.354503 secs Process 22520) Received: from it-phil.weatherbeeta.com.au (HELO wbaus090) (172.20.0.155) by mail.weatherbeeta.com.au with SMTP; 27 Apr 2007 10:27:54 +1000 From: "Phillip Smith" To: "'Steve Midgley'" , Subject: Re: Selecting rows with "static" ordering Date: Fri, 27 Apr 2007 10:27:41 +1000 Organization: Weatherbeeta Pty Ptd Message-ID: <005501c78862$de21d440$9b0014ac@wbaus090> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook, Build 10.0.6626 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2962 Importance: Normal In-Reply-To: <20070427001004.B5F5D9FBABA@postgresql.org> X-Scanned-By-wbeetaau1: Virus scan performed by network-box X-Scanned-By-wbeetaau1: Scanner file id is wbeetaau1-1177633681.909-20183-000 X-Scanned-By-wbeetaau1: No known viruses found in message (received+scanned in 0.01/0.09 secs) X-Scanned-By-wbeetaau1: Spam-Check-Result: No, hits=0 required=7 tests= autolearn=no version=2.0 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200704/270 X-Sequence-Number: 28270 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 =3D '3'; SELECT '2' AS ordering, t1.* INTO TEMP TABLE work_table FROM t1 WHERE = t1.id =3D '2'; SELECT '3' AS ordering, t1.* INTO TEMP TABLE work_table FROM t1 WHERE = t1.id =3D '5'; SELECT '4' AS ordering, t1.* INTO TEMP TABLE work_table FROM t1 WHERE = t1.id =3D '1'; SELECT '5' AS ordering, t1.* INTO TEMP TABLE work_table FROM t1 WHERE = t1.id =3D '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=20 am searching for a list of "id's" for a given table (these id values=20 are generated at run-time and held statically in an application-local=20 variable). From that application, I want to retrieve all those rows, and I want=20 them in the order they are currently stored in that variable. So take=20 for example this foreign application variable: ids =3D "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=20 semi-random (i.e. unpredictable/unreliable) order. And I don't want to=20 just "order by id" - I want to "order by id(3,2,5,1,4)" (if you see=20 what I mean) Is there a "neat trick" that anyone knows for pulling this off in a=20 single query? Basically right now I'm issuing 5 queries to the backend=20 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