Correct sorting of mysql elements

For example, I have this data in column number: M1, M2, ... M9, M10, U1, U2. In the MySQL query I have: ORDER BY number ASC, but the order display as: M1, M10, M2 ... U1, U2. What can I do to make the order as follows: M1, M2, ... M9, M10, U1, U2? Is this possible?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

If you strip the first character, then cast the result to its integer value, you can do this:

...ORDER BY SUBSTRING(mycolumn, 1, 1),
            SUBSTRING(mycolumn, 2) + 0

Updated to show sorting by letter, then by numeric value following the letter.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x