Solved

MySQL order by List

Posted on 2011-03-04
6
680 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:Cornelia Yoder
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 9

Expert Comment

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

Assisted Solution

by:rawinnlnx9
rawinnlnx9 earned 250 total points
ID: 35038295
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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 35038348
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 27

Author Closing Comment

by:Cornelia Yoder
ID: 35038984
Thanks, guys!
0
 
LVL 9

Expert Comment

by:rawinnlnx9
ID: 35040359
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:Cornelia Yoder
ID: 35040496
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

627 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