现有学员张三,李四,ID分别为1和2;
求HQL语句查询:所有同时教了张三和李四的老师集合。
FROM Teachar ... 后面条件怎么写- -?求大神赐教...
public class Teachar{
private int id;
private String name;
private Set<Relation> relations = new HashSet<Relation>(0);
}
public class Student{
private int id;
private String name;
private Set<Relation> relations = new HashSet<Relation>(0);
}
public class Relation{
private int id;
private Teachar teachar;
private Student student;
private int state;
private ...
}
------最佳解决方案--------------------
那就继续and r.teacher.id in(查出教王五的老师id) and r.teacher.id in(查出赵六的老师id)....
因为你这个需求是要取他们的交集就是都要存在。 ------其他解决方案--------------------
select b.teachar from Relation b where b.teachar.id in ((select a.teachar.id from Relation a where a.student.id=1)) and b.student.id=2;
------其他解决方案-------------------- from Teacher t where t.id in(select r.teacher.id from Relation r where r.student.id=1 or r.student.id=2); ------其他解决方案-------------------- from Teacher t where t.student.id in (1,2); ------其他解决方案-------------------- 1楼没看懂...应该是FROM Teachar
但是2楼和3楼的语句貌似不对啊,
你们这个语句好像查的是“教了张三或者李四的老师”...而不是并且 ------其他解决方案-------------------- 按楼主的需求是在studenId=1中的又要存在于studenId=2中那么
from Teacher t where t.id in(select r.teacher.id from Relation r where r.student.id=1 and r.teacher.id in(select rt.teacher.id from Relation rt where r2.student.id=2)); ------其他解决方案--------------------