Jump to content

Archived

This topic is now archived and is closed to further replies.

tjhilder

ORDER BY not ordering properly

Recommended Posts

Hi,

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

the first 10 results turn out like this: (the last number which looks like (7) is the views results.)
[code]
# 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)
[/code]

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

whats wrong?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[!--quoteo(post=364334:date=Apr 13 2006, 05:43 AM:name=tjhilder)--][div class=\'quotetop\']QUOTE(tjhilder @ Apr 13 2006, 05:43 AM) [snapback]364334[/snapback][/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 :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.