【问题标题】:how to apply having, group by clause while using join for MySQL table如何在对 MySQL 表使用 join 时应用有,group by 子句
【发布时间】:2016-03-05 20:52:09
【问题描述】:

我在 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;

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

【问题讨论】:

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


    【解决方案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 子句中使用逗号。

    【讨论】:

    猜你喜欢
    • 2019-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-16
    • 2016-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多