我想选择所有通过semester
1 和 4 的学生,我的查询只显示学生一行,我是期待两个人。
SELECT Student.mat_stud, fname, lname, dbirth, materials_notes, semester
FROM Student, Notes
WHERE Notes.mat_stud = Student.mat_stud
AND fails_status = 1
AND Notes.mat_div = 1
AND semester IN(1 , 4)
AND Notes.level = 1
AND school_year = 2015
Group By Notes.mat_stud
Having count(fname) = 2
Order By Notes.mat_stud
我的数据库表如下所示:
Student
-------------------------
mat_stud fname lname
-------------------------
142-3698 fname name
142-3699 fname name
142-3701 fname name
142-3700 fname name
Notes
---------------------------------------------------------------
mat_stud materials_notes semester level school_year
---------------------------------------------------------------
142-3698 1 1 2015
142-3699 1 1 2015
142-3701 1 1 2015
142-3698 4 1 2015
142-3700 1 1 2015
142-3700 4 1 2015
预期结果:
mat_stud fname lname materials_notes semester
142-3698 fname name 1
142-3698 fname name 4
142-3700 fname name 1
142-3700 fname name 4
答案 0 :(得分:0)
我通过加入已经过第一学期和第四学期的所有学生的成绩来获得这个解决方案:
SELECT Notes.mat_stud FROM Notes
WHERE semester IN (1, 4) Group By mat_stud Having COUNT(*) = 2
与所有学生一起。
SELECT t1.mat_stud, fname, lname, materials_notes FROM Student as t1, Notes
INNER JOIN ( SELECT Notes.mat_stud FROM Notes
WHERE semester IN (1, 4) Group By mat_stud Having COUNT(*) = 2) as t2
ON t2.mat_stud = t1.mat_stud
WHERE fails_status = 1
AND Notes.mat_div = 1
AND Notes.level = 1
AND school_year = 2015
AND t1.mat_stud = Notes.mat_stud
Order By Notes.mat_stud
答案 1 :(得分:0)
select S.mat_stud, S.fname, S.lname, N.materials_notes, N.semester
from Notes N, student S
where N.mat_stud = S.mat_stud and N.mat_stud in (
select mat_stud from Notes where semester in(1,4)
group by mat_stud
having count(mat_stud) > 1
)