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:
which is not desired, it should be:

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: ,

The URI to TrackBack this entry is:

RSS feed for comments on this post.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: