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.
The following query returns one row with two fields, containing the reply count and thread count for the given forum (replace
1with the actual forum ID):You can also use this syntax:
Or even this:
BTW, you’d need to properly support the query with indexes for good performance. At the first glance, this includes indexing
Replies.ThreadIdandThreads.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).