Solved

MySQL order by List

Posted on 2011-03-04
6
645 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
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 142

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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 27

Author Closing Comment

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
[MYSQL]: Delete is very slow 4 70
mysql update statement 3 22
Display images from mysql blob type (Not working) 9 28
Upgrade MySQL 5.5 to MySQL 5.6 on Windows 13 25
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikipedia.org/wiki/PHP  Very powerful.  But a…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

770 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