Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 739
  • Last Modified:

MySQL order by List

Is there any way to give a sort order by list in MySQL?

Something like
SELECT * FROM Table ORDER BY Id=(2,4,3,1,5)
effectively sorting the results in that order of ID value?
0
Cornelia Yoder
Asked:
Cornelia Yoder
  • 3
  • 2
2 Solutions
 
rawinnlnx9Commented:
Select * From Table ORDER BY Table.ID ASCENDING
0
 
rawinnlnx9Commented:
http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/Q_20244268.html

I googled on "do custom orderby SQL" and that was the topmost hit.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
I do it like this:

SELECT * FROM Table ORDER BY CASE WHEN Id= 2 THEN 1 WHEN ID = 4 THEN 2 WHEN ID = 3 THEN 3 WHEN ID = 1 THEN 4 WHEN ID = 5 THEN 5 ELSE 6 END 

Open in new window



0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Cornelia YoderArtistAuthor Commented:
Thanks, guys!
0
 
rawinnlnx9Commented:
I guess I don't get why I was assisted. I linked to the exact same answer the person after me gave. Just because I dind't copy and paste it I'm "assisted"? That doesn't strike me as assisted.
0
 
Cornelia YoderArtistAuthor Commented:
The answers were essentially the same, at approximately the same time.  I split the points evenly.  Is there some advantage to being the Accepted vs Assisted answer?
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.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now