#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    204
    Rep Power
    3

    Get postcount for a topic. also if there is none


    Hi .

    I have 2 tables, TOPICS and POSTS.. SO topics can have posts etc. I have this query to count the posts in a topic.
    Code:
    SELECT name, COUNT(posts.id) as replies FROM topics LEFT JOIN posts ON topics.id = posts.topicid
    But it only shows topics that HAVE POSTS, How can i also show the topics with no replies

    Thanks!
    Last edited by notflip; May 11th, 2013 at 06:53 AM.
  2. #2
  3. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,239
    Rep Power
    4279
    add GROUP BY name to the query
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

IMN logo majestic logo threadwatch logo seochat tools logo