'num_of_cmt' 是 4 而不是 2。为什么?

'num_of_cmt' is 4 instead of 2. Why?

我有 4 张桌子:

用户(id、姓名、电子邮件);

id  |   name    | email
1   |   ABC     | abc@gmail.com
2   |   XYZ     | xyz@gmail.com
3   |   AAA     | aaa@yahoo.com

papers(id, title, content, created_by)

id  |   title               |   content             |   created_by
1   |   This is title 1     |   This is content 1   |   1
2   |   This is title 2     |   This is content 2   |   1
3   |   This is title 3     |   This is content 3   |   3
4   |   This is title 4     |   This is content 4   |   1
5   |   This is title 5     |   This is content 5   |   3
6   |   This is title 6     |   This is content 6   |   2

评级(id,paperId,星)

id  |   paperId     |   star
1   |   1           |   2
2   |   2           |   4
3   |   3           |   4
4   |   2           |   2
5   |   1           |   3

评论(id、paperId、msg)

id  |   paperId     |   msg
1   |   1           |   abcd
2   |   2           |   xxxx
3   |   2           |   yyyy
4   |   3           |   zzzz
5   |   1           |   tttt
6   |   4           |   kkkk

我想获取字段:papers.id、papers.title、papers.content、users.name、 平均(rating.star),计数(comments.msg)

然后我执行如下查询:

SELECT papers.id, papers.title, papers.content, users.name,
AVG(rating.star) AS avg_star , COUNT(comments.msg) AS num_of_cmt 
FROM papers
JOIN users ON users.id = papers.created_by
LEFT JOIN rating ON rating.paperId = papers.id
LEFT JOIN comments ON comments.paperId = papers.id    
WHERE papers.id = 1

然后 "num_of_cmt" 字段的结果为假:

id  title               content            name     avg_star    num_of_cmt
1   This is title 1     This is content 1   ABC     2.5000       4

上面,'num_of_cmt'是4而不是2。为什么?

ratingscomments 都有多行 paperid = 1。因此,连接表会产生四个结果,具有以下 id:

ratings    comments
   1          1
   1          5
   5          1
   5          5

因此,计数为 4。您可以通过执行 count(distinct comments.id) 来修正计数。但是,平均值将会下降。

解决此问题的一种方法是在子查询中聚合 ratingscomments