Jump to content

ORDER BY not ordering properly


tjhilder

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?
Link to comment
https://forums.phpfreaks.com/topic/7174-order-by-not-ordering-properly/
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.
[!--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 :)

Archived

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

×
×
  • Create New...

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.