Re: selecting data from subquery in same order

From: "Scott Marlowe" <scott(dot)marlowe(at)gmail(dot)com>
To: mark <markkicks(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: selecting data from subquery in same order
Date: 2008-08-17 02:07:40
Message-ID: dcc563d10808161907q70e6bf75ma1488019a7702deb@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sat, Aug 16, 2008 at 8:07 PM, Scott Marlowe <scott(dot)marlowe(at)gmail(dot)com> wrote:
> On Sat, Aug 16, 2008 at 6:11 PM, mark <markkicks(at)gmail(dot)com> wrote:
>> hi
>> if i execute this statement:
>>
>> select * from users where id in (2341548, 2325251, 2333130, 2015421,
>> 2073536, 2252374, 2273219, 2350850, 2367318, 2032977, 2032849, )
>>
>> the order of rows obtained is random.
>>
>> is there anyway i can get the rows in the same order as the ids in
>> subquery? or is there a different statement i can use?
>> thanks!
>
> Technically, that's just a list, not a subquery, but that's not
> important right now.
>
> You can use a case statement.
>
> select field1, field2, idfield from users where id in (1,4,3) order by
> case
> when idfield=1 then 1
> when idfield=3 then 2
> when idfield=4 then 3
> end

oops, that should be

when idfield=1 then 1
when idfield=4 then 2
when idfield=3 then 3

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Amber 2008-08-17 07:08:31 Re: What's size of your PostgreSQL Database?
Previous Message Scott Marlowe 2008-08-17 02:07:09 Re: selecting data from subquery in same order