I have a table with a username as unique & primary key.
I ran this Query to order them in ascending order
ALTER TABLE `table` ORDER BY `Username` ;
So the rows are ordered like this
+-----------+----------+
| Username | Password |
+-----------+----------+
| soho16793 | test1 |
| soho4595 | test2 |
| soho504 | test3 |
| soho931 | test4 |
+-----------+----------+
But I want them like this
+-----------+----------+
| Username | Password |
+-----------+----------+
| soho504 | test3 |
| soho931 | test4 |
| soho4595 | test2 |
| soho16793 | test1 |
+-----------+----------+
How can I achieve this ?
Best Answer
I created a table called "billy".
which doesn't give the correct sort order - 1234 is bigger than 222!
Then, from here, I got the SUBSTRING() function and formulated the following SQL:
which gives you the integers you require to sort on ("fred" is an alias for the new integer column):
This also helped for CASTing. And thanks to @oNare for his question in comments below which led me to change this post from using the MID() function to the more standard SUBSTRING().
And the answer you require is given by