Reputation: 317
I have an SQL query where I am taking an arbitrary list of ids that may be in any order that I want to fetch.
select * from apps where id in (3,5,1);
This works as desired. How can I make a query that also orders the apps by this list, like this (Which doesn't work)? Or do I have to sort these in the rest of my program?
select * from apps where id in (3,5,1) order by (3,5,1);
Upvotes: 0
Views: 89
Reputation: 1269923
You can use a case
statement for this:
select *
from apps
where id in (3, 5, 1)
order by (case id when 3 then 1
when 5 then 2
when 1 then 3
end);
Upvotes: 2