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
为了找到重复的学生,我尝试了查询
select * from Student group by Name having count(*)>1
,得到SudentId Name
_______________________
101 Peter
116 Peter
117 Peter
实际上我需要过滤具有相同主题的学生。 IE,
101 Peter
116 Peter
是重复的(同一主题)但不是
117 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)
关于mysql - 从与另一个表有关联的表中删除重复项,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40503450/