user2769165
user2769165

Reputation: 69

Count Statement in mysql select

I have query which like to count the number of post in each thread. Now I faced some problem. If my new thread which don't have the post id it will not display out the thread. May I know how can I fix it? Here is my query

SELECT t.title, m.userName, COUNT(p.postID) AS Expr1, t.threadID 
FROM post p ,thread t , person m WHERE m.PersonID = t.PersonID 
AND t.threadID = p.threadID AND t.categories = "Announcement"
GROUP BY t.title, m.PersonName

I hope i will get the result like

-------------------------------------------------
| Title          |   Author |            Replies|
-------------------------------------------------
| Hello          |  haah    |       7           |
 ------------------------------------------------
| Bye Bye        |  lee     |       8           |
 ------------------------------------------------

The replies is based on the replies of thread.

Upvotes: 1

Views: 764

Answers (3)

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79979

Use LEFT JOIN instead, something like this:

SELECT 
  t.title, m.userName, t.threadID, 
  COUNT(COALESCE(p.postID, 0)) AS Expr1
FROM 
(
   SELECT * 
   FROM  thread 
   WHERE categories = 'Announcement'
) AS t
LEFT JOIN post p ON t.threadID = p.threadID
LEFT JOIN person m ON m.PersonID = t.PersonID 
GROUP BY t.title, m.PersonName, t.threadID ;

Then if a thread doesn't have any posts, it will be included in the result set with COUNT = 0.

Upvotes: 1

gvee
gvee

Reputation: 17171

First step is to re-write your query using JOIN syntax (you need to learn this)

SELECT t.title
     , m.userName
     , COUNT(p.postID) As Expr1
     , t.threadID 
FROM   thread As t
 INNER
  JOIN post As p
    ON p.threadID = t.threadID
 INNER
  JOIN person As m
    ON m.PersonID = t.PersonID 
WHERE  t.categories = "Announcement"
GROUP
    BY t.title
     , m.PersonName

This uses INNER joins. This join type indicates that every record must have a match on either side of the join.

To get the result you're looking for you need an OUTER join. This will return all records from one side of the join and match up, where possible, the records from the outer table.

SELECT t.title
     , m.userName
     , COUNT(p.postID) As Expr1
     , t.threadID 
FROM   thread As t
 LEFT
  JOIN post As p
    ON p.threadID = t.threadID
 INNER
  JOIN person As m
    ON m.PersonID = t.PersonID 
WHERE  t.categories = "Announcement"
GROUP
    BY t.title
     , m.PersonName

Note the very subtle change in the second query. I have changed one join to be a LEFT join. It will take all records in the first(left) table in the join, thread, and match up the post where possible.

Upvotes: 1

DevelopmentIsMyPassion
DevelopmentIsMyPassion

Reputation: 3591

SELECT t.title, m.userName, COUNT(p.postID) AS Expr1, t.threadID 
FROM post p  left join thread t on t.threadID = p.threadID 
left join person m  on  m.PersonID = t.PersonID 
Where t.categories = "Announcement"
GROUP BY t.title, m.PersonName

Upvotes: 0

Related Questions