Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sorting a Alphanumeric List in MySQL

Posted on 2015-01-16
1
Medium Priority
?
102 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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 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 is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Creating and Managing Databases with phpMyAdmin in cPanel.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
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 Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

916 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