select * from (
select count(1) nm,stu.学校id,stu.哪一级id,stu.班级
from student stu
where stu.id=A.id or stu.id=B.id
group by stu.学校id,stu.哪一级id,stu.班级) b where b.nm>1
应该有一个学生所属班级的历史表对吧,那么问题就简单了
假设这张表为T表,
select 班级相关信息 from t where 学生主键=ID
能查出该学生的历史班级信息
那么,我们取两个学生的交集
select 班级相关信息 from t where 学生主键=学生A
intersect
select 班级相关信息 from t where 学生主键=学生B
查询结果,即重合的班级,就是他们同为同学的班级,如果没有返回记录,则没有同学关系