Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6830565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:37:59+00:00 2026-05-26T22:37:59+00:00

I am trying to make a forum post count. I have two tables: Threads

  • 0

I am trying to make a forum post count. I have two tables:

Threads and Replies.

The Threads table is structured like this:

Id
ForumId
Last Poster
Title
Text

The Replies table is structured like so:

Id
ThreadId
Text

What I’m trying to do is get the total number of replies and threads in one ForumId.
I’ve looked all over Google, and still no luck.

How would I go about doing this?

The reason I am doing this is because I have too many posts and my old method was slow.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:37:59+00:00Added an answer on May 26, 2026 at 10:37 pm

    The following query returns one row with two fields, containing the reply count and thread count for the given forum (replace 1 with the actual forum ID):

    SELECT COUNT(*) ReplyCount, COUNT(DISTINCT ThreadId) ThreadCount
    FROM Replies
    WHERE ThreadId IN (SELECT Id FROM Threads WHERE ForumId = 1)
    

    You can also use this syntax:

    SELECT COUNT(*) ReplyCount, COUNT(DISTINCT ThreadId) ThreadCount
    FROM Replies JOIN Threads ON Replies.ThreadId = Threads.Id
    WHERE ForumId = 1
    

    Or even this:

    SELECT 
        (
            SELECT COUNT(*)
            FROM Replies JOIN Threads ON Replies.ThreadId = Threads.Id
            WHERE ForumId = 1
        ) ReplyCount,
        (
            SELECT COUNT(*) FROM Threads
            WHERE ForumId = 1
        ) ThreadCount
    

    BTW, you’d need to properly support the query with indexes for good performance. At the first glance, this includes indexing Replies.ThreadId and Threads.ForumId (in addition to “normal” PK indexes). For a nice introduction to the topic of indexing and performance, I warmly recommend reading Use The Index Luke!

    But also beware of the cost you pay for secondary indexes, in case you are using the InnoDB storage engine (see the “Disadvantages of clustering” section in this article).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have searched for two weeks, every night, exhaustively reading forum threads and trying
I'm trying to make a pull down menu post a form when the user
Now I have my website built on PHP & Mysql. Consider this like a
I'm trying to make a quick sample Backbone.js app that has a Post model,
i'm trying to make this form sticky but it's not doing. What i'm trying
I have a few linked tables in my custom forum: categories , sub_categories and
I am trying to make a simple upload file system, and this is my
I have been trying to make a user login page that, when the user
I'm trying to make Joomla's JFBConnect post on the user's FB wall some of
I'm trying to make this simple code of creating 3 multiple-choice question out of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.