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

时间:2015-01-17 01:10:10

标签: mysql sql left-join

我有4张桌子:

用户(身份证,姓名,电子邮件);

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

论文(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,star)

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,     AVG(rating.star),COUNT(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.为什么?

1 个答案:

答案 0 :(得分:2)

ratingscomments都有paperid = 1的多行。因此,加入表格会产生四个结果,其中包含以下ID:

ratings    comments
   1          1
   1          5
   5          1
   5          5

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

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