I am used MySQL 5.5 to do my query. I am facing the problem to sort the number with - in front of the character. Below is my example table and I want to sort the column number_with_name with the condition:
Table name: test123
+-----+--------------------+
| id | number_with_name |
+-----+--------------------+
| 1 | 200-2 David |
| 2 | 200-2-2 Peter |
| 3 | 200-2-2-9 James |
| 4 | 200 Robert |
| 5 | 200-2-3 Siva |
| 6 | 200-2-5 Denny |
| 8 | 200-2-9 Rose |
| 9 | 200-3 Kiki |
| 10 | 100-3-2 Viva |
| 11 | 100-3-15 Proton |
| 12 | 100-3-6 Saga |
| 13 | 100 Liver |
| 14 | 100-3 Shawn |
| 15 | 100-3-5-1 Kola |
| 16 | 100-3-5-8 Frankie|
| 17 | 100-3-5 Jala |
+----+---------------------+
I want the expected result like below the table:
+-----+--------------------+
| id | number_with_name |
+-----+--------------------+
| 13 | 100 Liver |
| 14 | 100-3 Shawn |
| 10 | 100-3-2 Viva |
| 17 | 100-3-5 Jala |
| 15 | 100-3-5-1 Kola |
| 16 | 100-3-5-8 Frankie|
| 12 | 100-3-6 Saga |
| 11 | 100-3-15 Proton |
| 4 | 200 Robert |
| 1 | 200-2 David |
| 2 | 200-2-2 Peter |
| 3 | 200-2-2-9 James |
| 5 | 200-2-3 Siva |
| 6 | 200-2-5 Denny |
| 8 | 200-2-9 Rose |
| 9 | 200-3 Kiki |
+----+---------------------+
I have used below the SQL to sort, but it doesn't work.
SELECT * from test123 order by SUBSTRING_INDEX(number_with_name, '-', -1) + 0 asc
Akira answer result for real test:
Hope someone can guide me on how to sort like this case. Thanks.
fiddle