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

Re: Ordering returned rows according to a list

From: "Adam Rich" <adam(dot)r(at)sbcglobal(dot)net>
To: "'Sebastian Tennant'" <sebyte(at)smolny(dot)plus(dot)com>,<pgsql-general(at)postgresql(dot)org>
Subject: Re: Ordering returned rows according to a list
Date: 2008-12-22 17:32:45
Message-ID: 005801c9645b$4d7785d0$e8669170$@r@sbcglobal.net (view raw or flat)
Thread:
Lists: pgsql-general
> 
> Hi all,
> 
> I'd like to make a single query that returns a number of rows using a
> 'WHERE id IN (<list-of-ids>)' condition, but I'd like the rows to be
> returned in the order in which the ids are given in the list.
> 
> Is this possible?
> 

Depending on how many IDs you have in your list, you can accomplish this
with a CASE statement:

SELECT *
FROM MYTABLE
WHERE id IN (6, 9, 3)
ORDER BY CASE id 
WHEN 6 then 1
WHEN 9 then 2
WHEN 3 then 3 END










In response to

pgsql-general by date

Next:From: ChristopheDate: 2008-12-22 17:34:39
Subject: Re: How are locks managed in PG?
Previous:From: paulo matadrDate: 2008-12-22 17:22:38
Subject: Erro in vaccum

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