Current location: Hot Scripts Forums » Other Discussions » Database » Mysql Order by bug


Mysql Order by bug

Reply
  #1 (permalink)  
Old 03-17-07, 07:00 AM
djnaf's Avatar
djnaf djnaf is offline
Newbie Coder
 
Join Date: Mar 2004
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Mysql Order by bug

Hi,

I have a DB with lots of property information in it and I need to return all the properties and order them by price. However, I at the moment my query is sorted incorrectly:

c1
c10
c11
c12
c2
c20
c21
c3
c31

They should be more like:
c1
c2
c3
c11
c12
c20
c21
c31
How would I go about this? My existing query is as follows:

Code:
SELECT * FROM <table> ORDER BY itemno ASC

Thanks in advance...
__________________
Code Talking Forum
Programming & Scripts Forum
Reply With Quote
  #2 (permalink)  
Old 03-17-07, 09:18 AM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Actually I believe the sort orderis correct. You may need to specify an ASCII sort.

Quote:
Originally Posted by djnaf View Post
Hi,

I have a DB with lots of property information in it and I need to return all the properties and order them by price. However, I at the moment my query is sorted incorrectly:

c1
c10
c11
c12
c2
c20
c21
c3
c31

They should be more like:
c1
c2
c3
c11
c12
c20
c21
c31
How would I go about this? My existing query is as follows:

Code:
SELECT * FROM <table> ORDER BY itemno ASC

Thanks in advance...
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Reply With Quote
  #3 (permalink)  
Old 03-17-07, 09:23 AM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
This is not really a bug, it is how you have defined and stored your data.

Because the item numbers have an alphabetic character, they are no longer numbers. They are strings and I am sure your column type is a character/string type.

The reason for the "incorrect" order, is this is how strings are sorted, character by character, left to right. That they have numbers in them does not matter and has no meaning.

There are a number of ways you can fix this -

1) Keep them as strings, but make all the number fields the same length -
c01, c02, ... c09, c10, c11... This makes the same character position in each string have the same significance.

2) Remove the "c" from your item numbers and make the column an actual number type. You can also remove the "c" and leave this as a character type, but you must force the query to treat them as numbers to get it to sort correctly. If you want the "c" in the final results, you can either append it as part of the query or as part of the code that processes the results.

3) You can form a query that strips the "c" and converts this to a number to be used in the ORDER BY clause. The following query will work (tested) -
PHP Code:

$query 'SELECT itemno FROM your_table ORDER BY CONVERT(REPLACE(itemno,"c",""),UNSIGNED)'
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and MySQL ? rob2132 Hot Scripts Forum Questions, Suggestions and Feedback 4 08-29-08 02:22 AM
MySql Labyrinth moron Database 4 02-22-08 02:46 AM
How do you invert mysql results order ? ragnartm PHP 3 11-30-06 12:15 PM
Php Mysql Bug??? tranquilraven PHP 4 03-01-06 03:06 AM
mySQL SELECT, ORDER BY. Search, Find Next and Prev Brandon@Cstone PHP 4 09-28-05 02:26 AM


All times are GMT -5. The time now is 04:57 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.