Solved

MySQL order by List

Posted on 2011-03-04
6
622 Views
Last Modified: 2012-05-11
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
Comment
Question by:yodercm
  • 3
  • 2
6 Comments
 
LVL 9

Expert Comment

by:rawinnlnx9
Comment Utility
Select * From Table ORDER BY Table.ID ASCENDING
0
 
LVL 9

Assisted Solution

by:rawinnlnx9
rawinnlnx9 earned 250 total points
Comment Utility
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
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 27

Author Closing Comment

by:yodercm
Comment Utility
Thanks, guys!
0
 
LVL 9

Expert Comment

by:rawinnlnx9
Comment Utility
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
 
LVL 27

Author Comment

by:yodercm
Comment Utility
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

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Loading csv or delimited data files to MySQL database is a very common task frequently questioned about and almost every time LOAD DATA INFILE comes to the rescue. Here we will try to understand some of the very common scenarios for loading data …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now