如何在为MySQL表使用join时应用having,group by子句

时间:2016-03-05 20:52:09

标签: mysql join group-by aggregate-functions having

我在MySQL中有2个表 - 问题表

QuestionID | QuestionName
-----------|---------------------------------------
1          | How is your faculty communication
-----------|---------------------------------------
2          | How is your study matrial
-----------|---------------------------------------
3          | How your faculty language
-----------|---------------------------------------
4          | Is your faculty cooperative
-----------|---------------------------------------
5          | Is your practical time is enough
-----------|---------------------------------------
6          | Your class starts on time
-----------|---------------------------------------
7          | In practical your doubts are cleared properly
-----------|---------------------------------------
8          | What will you rate for computer
-----------|---------------------------------------
9          | In Theory your questions are replied properly
-----------|---------------------------------------
10         |Your faculty is comes on time
-----------|---------------------------------------

结果表

RID QID Faculty         Student         Sem Excell  Better  Good Poor
1   1   Ankush          Vishal Deb      III 1       0       0    0
2   2   Ankush          Vishal Deb      III 0       1       0    0
3   3   Ankush          Vishal Deb      III 0       0       1    0
4   4   Ankush          Vishal Deb      III 0       0       0    1
5   5   Ankush          Vishal Deb      III 0       0       1    0
6   6   Ankush          Vishal Deb      III 0       1       0    0
7   7   Ankush          Vishal Deb      III 1       0       0    0
8   8   Ankush          Vishal Deb      III 0       1       0    0
9   9   Ankush          Vishal Deb      III 0       0       1    0
10  10  Ankush          Vishal Deb      III 0       0       0    1
11  1   Mahendra Singh  Mohit Chauhan   III 0       1       0    0
12  2   Mahendra Singh  Mohit Chauhan   III 0       0       1    0
13  3   Mahendra Singh  Mohit Chauhan   III 0       1       0    0
14  4   Mahendra Singh  Mohit Chauhan   III 0       0       0    1
15  5   Mahendra Singh  Mohit Chauhan   III 0       1       0    0
16  6   Mahendra Singh  Mohit Chauhan   III 0       0       1    0
17  7   Mahendra Singh  Mohit Chauhan   III 1       0       0    0
18  8   Mahendra Singh  Mohit Chauhan   III 0       0       0    1
19  9   Mahendra Singh  Mohit Chauhan   III 0       1       0    0
20  10  Mahendra Singh  Mohit Chauhan   III 0       0       0    1

现在我需要显示特定学期的特定教师的记录,但报告应该显示该学期学生的优秀,更好,好和差的总数。

例如,如果来自第三学期的5名学生提交了针对Ankush的反馈,那么报告应该是 - 我为4个问题提供示例

---------------------------------------------------------------------+
Question                           |Excellent | Better | Good | Poor |
-----------------------------------|----------|--------|------|------|
How is your faculty communication  |    3     |   2    |   0  |  0   |
-----------------------------------|----------|--------|------|------|
How is your study matrial          |    1     |   1    |   3  |  0   |
-----------------------------------|----------|--------|------|------|
How your faculty language          |    0     |   1    |   3  |  1   |
-----------------------------------|----------|--------|------|------|
Is your faculty cooperative        |    1     |   1    |   2  |  1   |
-----------------------------------|----------|--------|------|------|

我尝试了这个查询,但这不是我需要的

SELECT q.questionname, r.excellent, r.better, r.good, r.poor
FROM question q, result r
WHERE r.facultyid =  'Ankush'
AND r.Semester =  'III'
AND q.questionID = r.questionID

也尝试了

Select q.questionname, sum(r.excellent),sum(r.better),sum(r.good),sum(r.poor)
from question q,result r 
where r.facultyid='Ankush' and r.Semester='III' and q.questionID=r.questionID;

但不成功。请指导我如何获得我的结果。 提前谢谢。

1 个答案:

答案 0 :(得分:1)

你的第二个查询非常接近 - 你刚刚离开了group by条款:

Select q.questionname, sum(r.excellent),sum(r.better),sum(r.good),sum(r.poor)
from question q 
    inner join result r on q.questionID=r.questionID
where r.facultyid='Ankush' 
    and r.Semester='III' 
group by q.questionname

另请注意,这使用明确的join。一般情况下,我建议不要在from子句中使用逗号。