mysql ORDER BY based on IN clause

I want to ORDER my sql query based on the IN clause

select * from acct where acct_num in (200,100,150) order by (200,100,150)

My result is ordered by
100,150,200
I assume because acct_num is an index.

I want my resulting order to be
200,100,150
pmsguyAsked:
Who is Participating?
 
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
you then need to write the order by like that:
select * from acct where acct_num in (200,100,150)
 order by case when acct_num = 200 then 0 when acct_num =  100 then 2 else 3 end

Open in new window

0
 
pmsguyAuthor Commented:
Is there an easier way to do this query?
The IN list will be dynamic.

0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
not that I know of.
but if the "IN" list will be dynamic, so can be your order by?
0
 
Ovid BurkeConsultant InstructorCommented:
Try this:
SELECT * FROM acct WHERE acct_num IN (200,100,150) ORDER BY FIELD(acct_num,200,100,150);

Open in new window

0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
actually, that is a good one!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.