Solved

Sorting a Alphanumeric List in MySQL

Posted on 2015-01-16
1
76 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 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

21 Experts available now in Live!

Get 1:1 Help Now