| От | Sebastian Tennant |
|---|---|
| Тема | Re: Ordering returned rows according to a list |
| Дата | |
| Msg-id | ej00nl5v.fsf@vps203.linuxvps.org обсуждение исходный текст |
| Ответ на | Ordering returned rows according to a list (Sebastian Tennant <sebyte@smolny.plus.com>) |
| Список | pgsql-general |
Quoth "Adam Rich" <adam.r@sbcglobal.net>: >> 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. >> > 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 Thanks for this suggestion Adam. You say "depending on how many IDs there are in the list". The query is constructed programatically so unless there's a limit on the number of conditions a CASE clause can handle, this is the way I'll go about it. Sebastian
В списке pgsql-general по дате отправления:
Сайт использует файлы cookie для корректной работы и повышения удобства. Нажимая кнопку «Принять» или продолжая пользоваться сайтом, вы соглашаетесь на их использование в соответствии с Политикой в отношении обработки cookie ООО «ППГ», в том числе на передачу данных из файлов cookie сторонним статистическим и рекламным службам. Вы можете управлять настройками cookie через параметры вашего браузера