Skip site navigation (1) Skip section navigation (2)

Re: Order by provided IDs?

From: Thomas Kellerer <spam_eater(at)gmx(dot)net>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: Order by provided IDs?
Date: 2010-07-14 18:22:44
Message-ID: i1kv9i$7gc$1@dough.gmane.org (view raw or flat)
Thread:
Lists: pgsql-novice
Mark Kelly wrote on 14.07.2010 20:03:
> Hi.
>
> If my query is:
>
> SELECT art_id, art_headline, art_date
> FROM article
> WHERE art_id IN (357, 344, 120, 258, 369, 195, 343, 370);
>
> can I ask Postgres to return the rows in the order of the IDs in the IN
> clause? They are in a specific order defined outside the database.
>

Something like:

SELECT art_id, art_headline, art_date
FROM article a
  JOIN (values (1,357), (2,344), (3, 120), (4, 258), (5, 369), (6, 195), (7, 343), (8, 370) ) t(sort_order, id) ON t.id = a.art_id
ORDER BY t.sort_order


In response to

Responses

pgsql-novice by date

Next:From: Mark KellyDate: 2010-07-14 19:13:20
Subject: Re: Order by provided IDs?
Previous:From: Mark KellyDate: 2010-07-14 18:03:25
Subject: Order by provided IDs?

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group