假设有 7 个这样的表:
class_teacher_lesson
class_teacher_lession_ID, class_ID, teacher_ID, lesson_ID
班级
class_ID, class
老师
teacher_ID, teacher
课
lesson_ID, lesson
学生
student_ID, class_ID, ..., ....
调查回答
survey_answer_ID, survey_question_ID, class_teacher_lession_ID, survey_answer
调查问题
survey_question_ID, survey_question,
结果应该是这样的:
我将通过一个循环得到这个:
数学课:
survey_question (where teacher_id = 11 and lesson_id = 13):
Total Students Total Students Answered Survey survey_answer AVG
155 45 79%
survey_question 2 (where teacher_id = 11 and lesson_id = 13):
Total Students Total Students Answered Survey survey_answer AVG
155 99 87%
英语课程:
survey_question (where teacher_id = 11 and lesson_id = 15):
Total Students Total Students Answered Survey survey_answer AVG
90 20 50%
survey_question 2 (where teacher_id = 11 and lesson_id = 15):
Total Students Total Students Answered Survey survey_answer AVG
90 25 34%
到目前为止,我已经尝试过:
SELECT teacher AS 'Teacher', class AS 'Class', lesson AS 'Lesson',
COUNT(student_ID) AS 'Number of students',
COUNT(enquete_antwoord_ID) AS 'Total students answered survey',
enquete_vraag AS 'Survey Question'
FROM Teacher
LEFT JOIN class_teacher_lesson
ON teacher.teacher_ID = class_teacher_lesson.teacher_ID
LEFT JOIN lesson
ON class_teacher_lesson.class_ID = lesson.lesson_ID
LEFT JOIN class
ON class_teacher_lesson.class_ID = class.class_ID
LEFT JOIN student
ON class.class_ID = student.class_ID
LEFT JOIN survey_answer
ON class_teacher_lesson.class_teacher_lesson_ID =
survey_answer.class_teacher_lesson
LEFT JOIN survey_question
ON survey_answer.survey_question_ID = survey_question.survey_question_ID
WHERE teacher.teacher_ID = '1158' AND lesson.lesson_ID = '449'
GROUP BY class.class, lesson.lesson, teacher.teacher,
survey_answer.survey_answer, survey_question
有了这个结果:
Teacher: Class: Lesson: Number of students: Filled in: Survey_question:
t1 c1 math 54 54 question1?
t1 c1 math 24 0 NULL
t1 c1 math 22 0 NULL
结果应该变成这样:
Teacher: Class: Lesson: Number of students: Filled in: Survey_question:
t1 c1 math 90 54 question1?