Sorting a number field in MySQL: Making order by work for number fields in mysql

When using Order By field having number doesnt work some time. Actually it tries to sort the fields considering them as string, so numbers 11,2,1,111,21,3 will be sorted like:
1,11,111,2,21,3
which is not desired, it should be:
1,2,3,11,21,111

To order the values as they should be, simply use the SQL ABS function.

to get this desired result use query like:

SELECT * FROM ‘myTable’ WHERE b=’abc’ ORDER BY ABS(price) ASC

here price is number field.

Published in: on November 18, 2009 at 11:36 am  Leave a Comment  
Tags: ,