SQL question..

Started by Alexis Maldonadoalmost 24 years ago2 messagesgeneral
Jump to latest
#1Alexis Maldonado
amaldona@ctcd.cc.tx.us

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

#2Petr Bravenec
pbravenec@solartec.cz
In reply to: Alexis Maldonado (#1)
Re: SQL question..

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:

Show quoted text

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?