Student
_______________________
SudentId Name
101 Peter
103 John
112 James
116 Peter
117 Peter
SudentId_Subject
_______________________
SudentId Subject
101 Physics
103 Chemistry
112 Mathematics
116 Physics
117 Commerce
为了找到重复的学生,我尝试了查询
从名为(*)> 1的名称中选择*来自学生组,得到
SudentId Name
_______________________
101 Peter
116 Peter
117 Peter
实际上我需要过滤同一主题的学生.即
101 Peter
116 Peter
是重复的(同一主题)但不是彼得
我该如何为此编写查询?
最佳答案 我可能有过复杂的事情,但想不出更简单的方法:
SELECT ss.student_id,ss.name
FROM student ss
JOIN studentid_subject tt
ON(ss.student_id = tt.student_id)
JOIN(SELECT t.name,s.department
FROM student t
JOIN studentid_subject s
ON(t.student_id = s.student_id)
GROUP BY t.name,s.department
HAVING COUNT(*) > 1) p
ON(ss.name = p.name and tt.department = p.department)