runnerjp Posted April 16, 2010 Share Posted April 16, 2010 if ($result = mysql_query("SELECT COUNT(lastposter) as cnt, SUM(numreplies) as sm FROM forumtutorial_posts WHERE `forum` = 'general' ")) { if (mysql_num_rows($result)) { $row = mysql_fetch_assoc($result); $topic = $row['cnt']; $posts = $row['cnt'] + $row['sm']; ?> Im trying to get num of topics in my forum in my db it looks like this forum Lastposter views general Admin 3 general 0 as you can see there is only 1 result in lastposter but for some reason when i get the results it echo 2 instead of 1... why? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted April 16, 2010 Share Posted April 16, 2010 COUNT doesn't care if the Lastposter column is empty. It only counts the number of rows selected and since you didn't specify that Lastposter equaling to the empty string shouldn't be selected, COUNT will of course count it as a result. If you want to only have 1 result, you would need to specify in your query to not select the rows where Lastposter is equal to the empty string. SELECT COUNT(*) AS cnt, SUM(numreplies) AS sm FROM forumtutorial_posts WHERE forum = 'general' AND Lastposter <> ''; Quote Link to comment Share on other sites More sharing options...
runnerjp Posted April 16, 2010 Author Share Posted April 16, 2010 but SUM(numreplies) will have the poster added... will i have to make it into 2 diff quesry? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted April 16, 2010 Share Posted April 16, 2010 No, it shouldn't have added it. You sure it did? Quote Link to comment 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.