Link to home
Start Free TrialLog in
Avatar of serviceco
serviceco

asked on

Sorting a Alphanumeric List in MySQL

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.
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial