Imad Posted November 20, 2008 Share Posted November 20, 2008 Hi Guys, I have a MySQL query going on here which is suppose to bring the top 20 threads based on the most views threads to the least. Here's the query: $query = "SELECT * FROM threads ORDER BY views DESC limit 20"; However, it doesn't seem to put the most viewed thread first to the least, it's just scattered around. It brings a 5,000 viewed thread second on the list compared to a 5 views thread. Any ideas? Best Regards. Link to comment https://forums.phpfreaks.com/topic/133443-greatest-to-least-help/ Share on other sites More sharing options...
networkthis Posted November 20, 2008 Share Posted November 20, 2008 did you try it without limit just to make sure it is printing out in DESC order? Link to comment https://forums.phpfreaks.com/topic/133443-greatest-to-least-help/#findComment-694050 Share on other sites More sharing options...
xtopolis Posted November 20, 2008 Share Posted November 20, 2008 Does it come out the right way from mysql? I tested the query really quick and in theory, it's fine. Perhaps something btwn mysql and php output is causing it to jumble? Link to comment https://forums.phpfreaks.com/topic/133443-greatest-to-least-help/#findComment-694052 Share on other sites More sharing options...
networkthis Posted November 20, 2008 Share Posted November 20, 2008 $query = "SELECT * FROM threads ORDER BY views DESC LIMIT 0, 20 "; the 0 will be the starting point and the limit will be 20 results Link to comment https://forums.phpfreaks.com/topic/133443-greatest-to-least-help/#findComment-694054 Share on other sites More sharing options...
networkthis Posted November 20, 2008 Share Posted November 20, 2008 your query works right on my side as well.......... Link to comment https://forums.phpfreaks.com/topic/133443-greatest-to-least-help/#findComment-694056 Share on other sites More sharing options...
EternalSorrow Posted November 20, 2008 Share Posted November 20, 2008 However, it doesn't seem to put the most viewed thread first to the least, it's just scattered around. It brings a 5,000 viewed thread second on the list compared to a 5 views thread. Any ideas? Best Regards. It could be because your field type for the view count is something like varchar when it should be integer (int). Link to comment https://forums.phpfreaks.com/topic/133443-greatest-to-least-help/#findComment-694057 Share on other sites More sharing options...
Imad Posted November 20, 2008 Author Share Posted November 20, 2008 I just removed the limit and it brought back the same results. If I do it by thread id it works perfectly fine, but when I do it with the views it messes up. EDIT: I figured the problem out. I didn't create the views column properly, I just replaced it to this: views int unsigned NOT NULL default '0', and it worked fine. Thanks for your help guys. EDIT again: @EternalSorrow, exactly right. Link to comment https://forums.phpfreaks.com/topic/133443-greatest-to-least-help/#findComment-694058 Share on other sites More sharing options...
dropfaith Posted November 20, 2008 Share Posted November 20, 2008 what format is the views column of your database? if its not int then desc will see the first number i thinkk so 5 ,5000, 3, 1,20202022, 50000 will display based on the first number of the field i think i read that somewhere Link to comment https://forums.phpfreaks.com/topic/133443-greatest-to-least-help/#findComment-694059 Share on other sites More sharing options...
Imad Posted November 20, 2008 Author Share Posted November 20, 2008 Thanks for the info. I hadn't set it to int so it caused the problem. Best Regards. Link to comment https://forums.phpfreaks.com/topic/133443-greatest-to-least-help/#findComment-694079 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.