Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MySQL order by List

Posted on 2011-03-04
6
Medium Priority
?
694 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 1000 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 1000 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

715 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