2009/08/28

sort string field in mysql

A clever way to sort a string field.

Q:
It needs to be ORDER BY attribute. So then we get alphabetical order of non-numerical string (column named attribute) and numerical order for 'attribute' combined with numerical and non-numerical characters. For example,

apple
blueberry
orange

AND

1 foot
2 feet
3 feet

A:
ORDER BY CAST(attribute AS UNSIGNED)

No comments:

Post a Comment