Sorting both Alpanumeric and Numeric column in mysql

MEGHA POOJARY :

Here is my table data:

 - file_number -   status
 - ABC099    -     running
 - ABC100    -     running
 - ABC101    -     removed
 - ...
 - ABC1001   -     removed
 - ABC1002   -     removed
 - ABC1003   -     running
 - BCA099    -     running
 - BCA100   -      removed
 - BCA101   -      running
 - ...
 - BCA1001   -     removed
 - BCA1002   -     running
 - BCA1003   -     running

This query returns the correct answer with file_number sorted:

SELECT file_number,status FROM table ORDER BY LENGTH(file_number) asc

But I want to sort the status column as well i.e all the running files should come first and removed files should be displayed at the end.

Expected result:

 - file_number  -  status
 - ABC099    -     running
 - ABC100    -     running
 - ABC1003   -     running
 - ...
 - BCA099    -     running
 - BCA101   -      running
 - ...
 - BCA1002   -     running
 - BCA1003   -     running
 - ABC101    -     removed
 - ABC1001   -     removed
 - ABC1002   -     removed
 - BCA100   -      removed 
 - BCA1001   -     removed
Barbaros Özhan :

You can cast file_number as signed in order to make your order by the numeric part. And then add status desc part into ORDER BY clause to provide the desired alphabetical order if you have only two options for status column :

SELECT file_number,status
  FROM `table` 
 ORDER BY status DESC, CAST(file_number AS signed)

Demo

Update : If you also have different letter combinations for prefixing the file numbers and want to order prominently by them, then consider adding this column into ORDER BY list as the first component :

SELECT file_number,status
  FROM `table` 
 ORDER BY status DESC, file_number, CAST(file_number AS signed)

Demo 2

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=169284&siteId=1