Sort mysql table column value by part of its value

I have a table samplewith a column name Idthat have values ​​like

BA001
BA002
CAB003
BA004
BA005

Now, when I apply order by idselect to my query, the result will look like

BA001
BA002
BA004
BA005
CAB003

But I need a conclusion in order

BA001
BA002
CAB003
BA004
BA005

Can this be achieved?

+4
source share
2 answers

Yes it is possible. Assuming the Id column is VARCHAR and you have a fixed number of characters as numbers in your identifier and they appear on the right, you can use the RIGHT function in MySQL.

SELECT * FROM sample ORDER BY RIGHT(Id, 3);
+3
source

try it

SELECT ID FROM Table1
Order By Right(ID,3),ID

Screenshot

+2
source

Source: https://habr.com/ru/post/1533107/


All Articles