php - MYSQL natural sorting not behaving as expected -
natural sorting isn't behaving expected on query. don't understand why. having looked @ site http://www.copterlabs.com/blog/natural-sorting-in-mysql/ , method works part. however, alpha part of 'code' means sorting occurs in odd way.
result
m1 .. m3 p1 .. p3 m10 .. m19 p10 .. p19
expected
m1 .. m3 m10 .. m19 p1 .. p3 p10 .. p19
code
'select * stock order length(code), code';
you sort first length of field. of course mixes result in wrong way. try splitting alphanumeric values , numbers
select * stock order substr(code, 1, 1), substr(code, 2, 99) * 1
*1
converts string number. use cast(substr(code, 2, 99) signed)
Comments
Post a Comment