Re: Problem with result ordering

From: "A(dot) Kretschmer" <andreas(dot)kretschmer(at)schollglas(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Problem with result ordering
Date: 2007-01-25 16:52:37
Message-ID: 20070125165237.GA3765@a-kretschmer.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

am Thu, dem 25.01.2007, um 16:45:23 +0100 mailte Thorsten Körner folgendes:
> Hi,
>
> when I fire the following query:
> select m_id, m_u_id, m_title, m_rating from tablename where m_id in (26250,
> 11042, 16279, 42197, 672089);
>
> I will get the same results in the same order, as in in the next query:
> select m_id, m_u_id, m_title, m_rating from tablename where m_id in
> (11042,42197,672089,26250,16279);
>
> I wonder, how it is possible, to retrieve the results in the same order, as
> queried in the list. The listed IDs are from an application outside the
> database.

a little trick;

store your order-definition and the where-condition in a separate table.

Our table:
test=*# select * from foo;
id | val
----+-------
1 | text1
2 | text2
3 | text3
4 | text4
5 | text5
(5 rows)

Our sort-order:
-- col a: the sort-order
-- col b: the where-condition
test=*# select * from o;
a | b
---+---
1 | 3
2 | 5
3 | 1
(3 rows)

test=*# select foo.id, foo.val from foo join o on foo.id=o.b order by o.a;
id | val
----+-------
3 | text3
5 | text5
1 | text1
(3 rows)

You can try it without this table, only with generate_series or so.

Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Jeremy Haile 2007-01-25 17:31:57 Re: Stats collector frozen?
Previous Message Kai Schlamp 2007-01-25 16:46:35 relationship in a table