Jump to content


Photo

ORDER BY not ordering properly


  • Please log in to reply
5 replies to this topic

#1 tjhilder

tjhilder
  • Members
  • PipPipPip
  • Advanced Member
  • 136 posts
  • LocationKent, UK

Posted 12 April 2006 - 07:48 AM

Hi,

I have this query:
$photos_query = "SELECT photo_url, album_name, album_id, views FROM v3_photos ORDER BY views DESC LIMIT 10";

the first 10 results turn out like this: (the last number which looks like (7) is the views results.)
# chorley05 - 001 (7)
# phoebepics06 - 005 (5)
# swwedding05 - 002 (5)
# frencon06 - 001 (4)
# frencon06 - 003 (3)
# chorley05 - 002 (12)
# frencon06 - 002 (1)
# jansvisit05 - 004 (0)
# jansvisit05 - 005 (0)
# jansvisit05 - 006 (0)

why is it ordering it by the 1st number and not the overall number? should start 12.. 7.. 5.. etc

whats wrong?

#2 jworisek

jworisek
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 12 April 2006 - 03:50 PM

I would bet it is becuase of your column type... Are you using something like a varchar or char for that column?

change your column to int and see if it still happens.

#3 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 13 April 2006 - 04:48 AM

Well, it's obviously a string type column, since the value being stored in of the form "(N)" -- which is why MySQL is doing a lexical sort, not a numerical sort. That is, "12" > "1" when you compare strings. The solution, of course, is not the store the formatting of the number in the table, and format in on the way out, or in PHP. That way, you can use a proper INT column, and MySQL will sort as you expect.

Hope that helps.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#4 tjhilder

tjhilder
  • Members
  • PipPipPip
  • Advanced Member
  • 136 posts
  • LocationKent, UK

Posted 13 April 2006 - 09:43 AM

thanks alot, I changed varchar for int and it's working perfectly now =) i put int as all column types that only have number data.

#5 desithugg

desithugg
  • Members
  • PipPipPip
  • Advanced Member
  • 281 posts
  • LocationScarborough

Posted 13 April 2006 - 08:07 PM

[!--quoteo(post=364334:date=Apr 13 2006, 05:43 AM:name=tjhilder)--][div class=\'quotetop\']QUOTE(tjhilder @ Apr 13 2006, 05:43 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
thanks alot, I changed varchar for int and it's working perfectly now =) i put int as all column types that only have number data.
[/quote]

umm i was just going to post for the same question but changed to int and works :)


#6 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 13 April 2006 - 08:15 PM

You may want to consider using SMALLINT in certain circumstances; takes up less space, and can store numbers up to ~65K when declared as UNSIGNED.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users