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 3357318
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:37:07+00:00 2026-05-18T02:37:07+00:00

I have 3 tables: forums id, name, description threads thread_id, forum_id, user_id, title, content,

  • 0

I have 3 tables:

  1. forums

id, name, description

  1. threads

thread_id, forum_id, user_id, title, content, views

  1. posts

post_id, thread_id, author_id, content, date

What I want to do is to get all the threads in a forum, and get the post count of every thread. So I get every thread (WHERE forum_id = whatever) and then I LEFT JOIN with the table posts so in order to count the results.
But something is not working. Here is my query:

SELECT t.*, u.nick, COUNT(p.post_id) AS postcount
  FROM
    threads t
  LEFT JOIN
    users u
    ON
       u.id = t.user_id  
  LEFT JOIN
     posts p
     ON
       p.thread_id = t.thread_id
  WHERE
     t.forum_id = $this->forumID

This query will only show (I think) the threads that have any post on it. I also tried using the GROUP BY statement but it makes MySQL error…

How can I solve this?

———– EDIT:
I tried adding GROUP BY t.thread_id, however, as I said before, MySQL errors:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘WHERE t.forum_id = 2’ at line 15

Full query:

SELECT t.*, u.nick, COUNT(p.post_id) AS postcount
  FROM
    threads t
  LEFT JOIN
    users u
    ON
       u.id = t.user_id  
  LEFT JOIN
     posts p
     ON
       p.thread_id = t.thread_id
  GROUP BY
     t.thread_id
  WHERE
     t.forum_id = $this->forumID

EDIT 2:

My bad, I put the GROUP BY statement where it wasn’t meant to be. It is now solved.

  • 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-18T02:37:07+00:00Added an answer on May 18, 2026 at 2:37 am

    GROUP BY was the right way to go, so just add: GROUP BY t.thread_id

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

Sidebar

Related Questions

i have this 3 tables forums_forum +-----+--------+-------------+-------+-----+ | fid | name | description |
I have tables as below. Table Messages message_id parent_id forum_id user_id Table Users user_id
I have two tables, one is a table of forum threads. It has a
I have tables Product ( ID, CategoryID, ... ) Category ( ID, Name, ..
Please click here for sample tables and description. I have three tables ‘PROJECTS’, 'PROJECTMANAGER'
Please click here for sample tables and description. I have three tables ‘PROJECTS’, 'PROJECTMANAGER'
I have two tables one named Person , which contains columns ID and Name
I am building a forum and I have two tables: Threads ------- ThreadID UsersID
I have two functions addCategory, and addArticle as follows : function addCategory(){ $title=$_POST['title']; $description=$_POST['description'];
I have 3 tables: artists{id,name} media{id,name,filename} media_artists{artist_id,media_id} I created the models with n-n relationships

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.