X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 6CEAA5294D for ; Wed, 29 Jun 2005 08:57:43 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 39134-02 for ; Wed, 29 Jun 2005 11:57:37 +0000 (GMT) Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by svr1.postgresql.org (Postfix) with ESMTP id 257CA529E4 for ; Wed, 29 Jun 2005 08:57:36 -0300 (ADT) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.3/8.13.3) with ESMTP id j5TBvNFB086613 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 29 Jun 2005 05:57:26 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.3/8.13.3) with ESMTP id j5TBvNHu031092; Wed, 29 Jun 2005 05:57:23 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.3/8.13.3/Submit) id j5TBvNkv031091; Wed, 29 Jun 2005 05:57:23 -0600 (MDT) (envelope-from mfuhr) Date: Wed, 29 Jun 2005 05:57:23 -0600 From: Michael Fuhr To: Russell Simpkins Cc: pgsql-sql@postgresql.org Subject: Re: ORDER records based on parameters in IN clause Message-ID: <20050629115723.GA31013@winnie.fuhr.org> References: <937a483c3b3a9809ec978a24b9d9af76@biglumber.com> <000b01c57c9c$66589170$6401a8c0@simpkinshome1> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <000b01c57c9c$66589170$6401a8c0@simpkinshome1> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.005 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200506/350 X-Sequence-Number: 21951 On Wed, Jun 29, 2005 at 07:19:22AM -0400, Russell Simpkins wrote: > > Order by id will not do what you want, but this should. > Select * from table where id = 2003; > Union all > Select * from table where id = 1342; > Union all > Select * from table where id = 799; > Union all > Select * from table where id = 1450; Note that the semicolons should be omitted everywhere except for at the end of the entire query. Also, although the implementation might happen to return rows in that order, the documentation states that it's not guaranteed to: http://www.postgresql.org/docs/8.0/static/queries-union.html "UNION effectively appends the result of query2 to the result of query1 (although there is no guarantee that this is the order in which the rows are actually returned)." As the documentation states elsewhere and as the SQL standard says, without ORDER BY rows are returned in an unspecified order. The above query works by accident, not by design, so although it might work today there's no guarantee that it'll work tomorrow. -- Michael Fuhr http://www.fuhr.org/~mfuhr/