Re: ORDER records based on parameters in IN clause

From: "Russell Simpkins" <russellsimpkins(at)hotmail(dot)com>
To: mike(at)fuhr(dot)org
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: ORDER records based on parameters in IN clause
Date: 2005-06-29 14:22:07
Message-ID: BAY103-F2C56F6C371B9B2FDEEDCBB5E00@phx.gbl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

fair enough. but a simple order by id would never work.

>From: Michael Fuhr <mike(at)fuhr(dot)org>
>To: Russell Simpkins <russellsimpkins(at)hotmail(dot)com>
>CC: pgsql-sql(at)postgresql(dot)org
>Subject: Re: [SQL] ORDER records based on parameters in IN clause
>Date: Wed, 29 Jun 2005 05:57:23 -0600
>
>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/
>
>---------------------------(end of broadcast)---------------------------
>TIP 2: you can get off all lists at once with the unregister command
> (send "unregister YourEmailAddressHere" to majordomo(at)postgresql(dot)org)

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Michael Fuhr 2005-06-29 14:33:30 Re: ORDER records based on parameters in IN clause
Previous Message Rod Taylor 2005-06-29 13:28:49 Re: ENUM like data type