tjhilder Posted April 12, 2006 Share Posted April 12, 2006 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.. etcwhats wrong? Quote Link to comment https://forums.phpfreaks.com/topic/7174-order-by-not-ordering-properly/ Share on other sites More sharing options...
jworisek Posted April 12, 2006 Share Posted April 12, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/7174-order-by-not-ordering-properly/#findComment-26267 Share on other sites More sharing options...
fenway Posted April 13, 2006 Share Posted April 13, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/7174-order-by-not-ordering-properly/#findComment-26463 Share on other sites More sharing options...
tjhilder Posted April 13, 2006 Author Share Posted April 13, 2006 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 Link to comment https://forums.phpfreaks.com/topic/7174-order-by-not-ordering-properly/#findComment-26516 Share on other sites More sharing options...
desithugg Posted April 13, 2006 Share Posted April 13, 2006 [!--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 :) Quote Link to comment https://forums.phpfreaks.com/topic/7174-order-by-not-ordering-properly/#findComment-26723 Share on other sites More sharing options...
fenway Posted April 13, 2006 Share Posted April 13, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/7174-order-by-not-ordering-properly/#findComment-26726 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.