Re: ORDER records based on parameters in IN clause

From: Dawid Kuroczko <qnex42(at)gmail(dot)com>
To: "M(dot)D(dot)G(dot) Lange" <mlange(at)dltmedia(dot)nl>, pgsql-sql(at)postgresql(dot)org
Subject: Re: ORDER records based on parameters in IN clause
Date: 2005-06-30 07:51:35
Message-ID: 758d5e7f05063000514ece9e33@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On 6/30/05, M.D.G. Lange <mlange(at)dltmedia(dot)nl> wrote:
> Another option would be:
> SELECT * FROM table WHERE id=2003 OR id=1342 OR id=799 OR id=1450;
> This should give you the results in the right order...

I don't think so...

create temporary table seq as select * from generate_series(1,20) as g(id);
select * from seq where id in (5,2,12);
id
----
2
5
12

select * from seq where id = 5 or id = 2 or id = 12;
id
----
2
5
12

It certainly doesn't work.

You have to order it by something, like:

select * from seq where id in(5,2,12) order by id=5 desc,id=2 desc,id=12 desc;
id
----
5
2
12

Regards,
Dawid

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message KÖPFERL Robert 2005-06-30 08:01:49 Re: ENUM like data type
Previous Message M.D.G. Lange 2005-06-30 07:34:59 Re: ORDER records based on parameters in IN clause