I would like to order a set like this:
- a 2
- a 1
- b 9
not like the normal ascending order:
- a 1
- a 2
- b 9
I other words, I have a single column with some text and sometimes a number i the end. I would like to have all the fields with a number in the end to be sorted descending on the numbers, but the leading text must still be sorted ascending.
The strings are not just 1 char long but of an unknown length and the numbers are also not only 1 char long, but also of an unknown length and not guaranteed to be in the end of the string it might look like: “Random event, 12th time”
If I have a string like “Random event, 12th time” I want it to appear before: “Random event, 11th time” in my result.
Hope there are some SQL guys who know the answer to this 🙂
you could create a function that isolates the number you want to order by. eg. something like this (found here MySQL strip non-numeric characters to compare)
and now run your sql like this:
SELECT * FROM myTable order by myString, STRIP_NON_DIGIT(myString) DESC;