同时使用NOT IN和NOT

时间:2017-08-17 13:32:04

标签: mysql

我正在尝试使用方法NOT ININ过滤所有有或没有成绩的学生。我为学生选择空成绩时使用了NOT IN IN我选择了成绩合格的学生。是否可以将它们全部放在一个查询中而不是两个查询中?因为我想同时选择它们然后在我班级的某个地方打电话。

查询IN

SELECT subject_mt.subject_id, student_mt.student_id,
   registration_mt.firstname, registration_mt.middlename, registration_mt.lastname,
   subject_mt.title,
   MAX(IF(g.gradingperiod_id = 7000, g.grade, ""))AS first,
   MAX(IF(g.gradingperiod_id = 7001, g.grade, "")) AS second,
   MAX(IF(g.gradingperiod_id = 7002, g.grade, "")) AS third,
   MAX(IF(g.gradingperiod_id = 7003, g.grade, "")) AS fourth,
   g.final

FROM faculty_schedule
INNER JOIN schedule_mt ON schedule_mt.schedule_id = faculty_schedule.schedule_id
INNER JOIN section_mt ON section_mt.section_id = schedule_mt.section_id
INNER JOIN section_student ON section_student.section_id = section_mt.section_id
INNER JOIN student_mt ON student_mt.student_id = section_student.student_id
INNER JOIN registration_mt ON registration_mt.registration_id = student_mt.registration_id
INNER JOIN subject_mt ON subject_mt.subject_id = schedule_mt.subject_id
INNER JOIN student_grade AS sg ON sg.student_id = student_mt.student_id
INNER JOIN grade AS g ON g.grade_id = sg.grade_id


//WHERE CLAUSE HERE

GROUP BY 
subject_mt.subject_id, student_mt.student_id,
registration_mt.firstname, registration_mt.middlename, registration_mt.lastname,
subject_mt.title,
g.final;

查询NOT IN

INNER JOIN student_grade AS sg ON sg.student_id = student_mt.student_id
INNER JOIN grade AS g ON g.grade_id = sg.grade_id

WHERE faculty_schedule.faculty_id = pIN_facultyId
AND schedule_mt.section_id = pIN_sectionId
AND sg.student_id IN (SELECT student_id FROM student_grade)

1 个答案:

答案 0 :(得分:1)

所以我试图在我的评论旁边贴出我的答案。我认为查询将使用LEFT JOIN看起来像这样,您需要过滤所有数据,无论是否有成绩。

SELECT subject_mt.subject_id, student_mt.student_id,
   registration_mt.firstname, registration_mt.middlename, registration_mt.lastname,
   subject_mt.title,
   MAX(IF(g.gradingperiod_id = 7000, g.grade, ""))AS first,
   MAX(IF(g.gradingperiod_id = 7001, g.grade, "")) AS second,
   MAX(IF(g.gradingperiod_id = 7002, g.grade, "")) AS third,
   MAX(IF(g.gradingperiod_id = 7003, g.grade, "")) AS fourth,
   g.final

FROM faculty_schedule
INNER JOIN schedule_mt ON schedule_mt.schedule_id = faculty_schedule.schedule_id
INNER JOIN section_mt ON section_mt.section_id = schedule_mt.section_id
INNER JOIN section_student ON section_student.section_id = section_mt.section_id
INNER JOIN student_mt ON student_mt.student_id = section_student.student_id
INNER JOIN registration_mt ON registration_mt.registration_id = student_mt.registration_id
INNER JOIN subject_mt ON subject_mt.subject_id = schedule_mt.subject_id
LEFT JOIN student_grade AS sg ON sg.student_id = student_mt.student_id
LEFT JOIN grade AS g ON g.grade_id = sg.grade_id

WHERE faculty_schedule.faculty_id = pIN_facultyId
AND schedule_mt.section_id = pIN_sectionId
AND sg.student_id IN (SELECT student_id FROM student_grade)

GROUP BY 
subject_mt.subject_id, student_mt.student_id,
registration_mt.firstname, registration_mt.middlename, registration_mt.lastname,
subject_mt.title,
g.final;