简体   繁体   English

mysql中的Count语句选择

[英]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. 如果我的新帖子没有帖子ID,则不会显示该帖子。 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. 答复基于线程的答复。

Use LEFT JOIN instead, something like this: 改为使用LEFT JOIN ,如下所示:

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. 然后,如果一个线程没有任何帖子,它将以COUNT = 0包含在结果集中。

First step is to re-write your query using JOIN syntax (you need to learn this) 第一步是使用JOIN语法重新编写查询(您需要了解这一点)

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. 这使用INNER联接。 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. 为了获得想要的结果,您需要一个OUTER加入。 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. 它将采用join, thread的第一个(左)表中的所有记录,并在可能的情况下匹配该post

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM