Re: Is it possible to make the order of output the same as the order of input parameters?

Поиск
Список
Период
Сортировка
От Sam Mason
Тема Re: Is it possible to make the order of output the same as the order of input parameters?
Дата
Msg-id 20100602114336.GQ20550@samason.me.uk
обсуждение исходный текст
Ответ на Is it possible to make the order of output the same as the order of input parameters?  ("m. hvostinski" <makhvost@gmail.com>)
Список pgsql-general
On Tue, Jun 01, 2010 at 06:16:06PM -0400, m. hvostinski wrote:
> I have a simple query like:
>
> SELECT * FROM customer WHERE id IN (23, 56, 2, 12, 10)
>
> The problem is that I need to retrieve the rows in the same order as the set
> of ids provided in the select statement. Can it be done?

Yes, you just need to make the order explicit:

  SELECT c.*
  FROM customer c, (VALUES
    (1,23), (2,56),
    (3, 2), (4,12),
    (5,10)) x(ord,val)
  WHERE c.id = x.val
  ORDER BY x.ord;

--
  Sam  http://samason.me.uk/

В списке pgsql-general по дате отправления:

Предыдущее
От: Stephen Frost
Дата:
Сообщение: Re: Is it possible to make the order of output the same as the order of input parameters?
Следующее
От: Andreas Kretschmer
Дата:
Сообщение: Re: Is it possible to make the order of output the same as the order of input parameters?