Solved

Sorting a Alphanumeric List in MySQL

Posted on 2015-01-16
1
82 Views
Last Modified: 2015-09-08
I want a sort of the following list of values the way it is displayed below:

240646_1
240646_1A
240646_2
240646_3
240646_5
240646_5A
240646_5B
240646_5C
240646_6
240646_6A
240646_7
240646_8
240646_9
240646_9A
240646_10
240646_11
240646_11A
240646_12

I used the following MySQL query to get this sort.

SELECT
`TableName`.quoteid,
CAST(IF (Right(Right(`TableName`.quoteid,(LENGTH(`TableName`.quoteid)-(LOCATE('_',`TableName`.quoteid)))),1) NOT REGEXP ('^[0-9]'),
Left( Right(`TableName`.quoteid,(LENGTH(`TableName`.quoteid)-(LOCATE('_',`TableName`.quoteid))))  
,(Length(Right(`TableName`.quoteid,(LENGTH(`TableName`.quoteid)-(LOCATE('_',`TableName`.quoteid)))))-1))
,Right(`TableName`.quoteid,(LENGTH(`TableName`.quoteid)-(LOCATE('_',`TableName`.quoteid))))) AS UNSIGNED)  AS quotesortid,

IF( Right(`TableName`.quoteid,1) REGEXP ('^[0-9]'),' ',Right(`TableName`.quoteid,1)) As altsortid

From `TableName`
Where jobid = 240646
Order By
quotesortid,
altsortid

Open in new window


Yet it seems to me that there must be a more efficent means of performing this sort.  My query works but it is clunky and seems very innefficent at over a tenth of a second to run.

Is there a more efficient means of performing this sort than I am using.
0
Comment
Question by:serviceco
1 Comment
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 40554834
the ONLY real and good way is to get the 2 (3) parts apart, means into 3 columns, having each the proper data type.

means this value:
240646_1A

should be splitted into this (actually using the expression you use in your query above):
240646  (numerical)
1  (numerical)
A  (text)

for any application, you could combine the 3 values into 1 (concat function), but for the querying/sorting, only that method will work efficiently.
your approach does work indeed, but is very intensive for the processing, and needs to be repeated every time
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MySQL Error 3 43
What does "flush tables;" do exactly? 7 100
when to use sequences in mysql 4 30
Sudden decrease in performance when updating mysql using classic asp 6 23
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.

831 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