Thread: SQL question..

SQL question..

From
"Alexis Maldonado"
Date:
I know this might be a dumb basic sql question but..
 
I have a query list of IDs ( 4,3,1,2,12,5,6,8,7,9,11)
 
I want to build a query that gets those IDs in the exact same order but when i do
 
Select * From Table
Where ID IN ( 4,3,1,2,12,5,6,8,7,9,11)
 
It gets all the IDs in random order..
 
how can i get them in the order listed?
 
Thanks,
Alex

Re: SQL question..

From
Petr Bravenec
Date:
It looks like an impossible thing in SQL. The only way to achieve
this may be an temporary table with added sorting information:

create temporary table tpmsort (linenum serial, id int4);
insert into tmpsort (id) values (4);
insert into tmpsort (id) values (3);
insert into tmpsort (id) values (1);
insert into tmpsort (id) values (2); .... in the order you requested

and then

select * from table, tmpsort
   where table.id = tmpsort.id
   order by tmpsort.linenum;

Petr Bravenec

Alexis Maldonado wrote:

>
>
> I have a query list of IDs ( 4,3,1,2,12,5,6,8,7,9,11)
>
>
>
> I want to build a query that gets those IDs in the exact same order
> but when i do
>
>
>
> Select * From Table
>
> Where ID IN ( 4,3,1,2,12,5,6,8,7,9,11)
>
>
>
> It gets all the IDs in random order..
>
>
>
> how can i get them in the order listed?
>