如何在使用 join for MySQL table 时应用 having, group by 子句
how to apply having, group by clause while using join for MySQL table
我在 MySQL 中有 2 个表 -
问题 Table
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
-----------|---------------------------------------
结果Table
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;
但是没有成功。请指导我如何获得结果。
提前谢谢你。
你的第二个查询非常接近——你刚刚离开了 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
子句中使用逗号。
我在 MySQL 中有 2 个表 - 问题 Table
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
-----------|---------------------------------------
结果Table
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;
但是没有成功。请指导我如何获得结果。 提前谢谢你。
你的第二个查询非常接近——你刚刚离开了 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
子句中使用逗号。