0

im using a while loop to show my latest forum topics and count some fields

I'm trying to do it in one query and here is my code :

  SELECT t.*,p.*,
       SUM(t.topic_approved='1') AS Amount_Of_Topics,
       SUM(t.topic_views) AS Amount_Of_Topic_Views,
       SUM(t.topic_replies) AS Amount_Of_Topic_Replies, 
       SUM(p.post_approved ='1') AS Amount_Of_Posts
    FROM  bb3topics t left join  bb3posts p ON t.topic_id=p.topic_id
    ORDER BY t.topic_last_post_id DESC LIMIT 10

problem :

this code shows only one forum topic and not the rest

is there anything wrong with my query code ?!

1
  • Salil is right, you probably need GROUP BY. Read about it. Commented May 29, 2010 at 10:12

2 Answers 2

3

Yes.

SUM is always used with GROUP BY clause

Sign up to request clarification or add additional context in comments.

1 Comment

That is not true. It is not always used with GROUP BY. But in this case it is indeed what the OP needs.
1

Yes you are missing GROUP BY.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.