Solved

Sorting a Alphanumeric List in MySQL

Posted on 2015-01-16
1
92 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
[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
1 Comment
 
LVL 143

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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP query / monitor data from Telnet to MySQL 8 128
Upgrade MySQL 5.5 to MySQL 5.6 on Windows 13 119
Duplicated data in GROUP_CONCAT 2 52
Building a glossary into your website 9 60
All XML, All the Time; More Fun MySQL Tidbits – Dynamically Generate XML via Stored Procedure in MySQL Extensible Markup Language (XML) and database systems, a marriage we are seeing more and more of.  So the topics of parsing and manipulating XM…
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 …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

751 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