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.

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

The URI to TrackBack this entry is: https://indiandeve.wordpress.com/2009/11/18/sorting-a-number-field-in-mysql-making-order-by-work-for-number-fields-in-mysql/trackback/

RSS feed for comments on this post.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: