Spring查询in()无效的地方

时间:2017-07-10 10:34:03

标签: sql spring spring-data-jpa

我正在尝试查询有3个主题的院系,mysql语句如下:

select * from faculty_subject where subject_id  in(1 and 3 and 2);

其中1,2,3是主题ID。我只想要拥有所有这些科目的课程,这样我才能使用和。但我不知道为什么我的春季查询不起作用。我尝试了不同的方法,但在这种情况下它的第一个主题id返回了院系1.这是我的春季查询

@Query(value = "select * from faculty_subject where subject_id in(?#{[0]} and ?#{[1]} and ?#{[1]})", nativeQuery =  true)
    List<FacultySubject> getFacultyBySubjects(long subjectTop, long subjectMid, long subjectBottom);

另一个无效的

 @Query(value = "select * from faculty_subject where subject_id in(?1 and ?2 and ?3)", nativeQuery =  true)
    List<FacultySubject> getFacultyBySubjects(long subjectTop, long subjectMid, long subjectBottom);

另一个查询

@Query(value = "select * from faculty_subject where subject_id in(:#{#subjectTop} and :#{#subjectMid} and :#{#subjectBottom})", nativeQuery =  true)
List<FacultySubject> getFacultyBySubjects(@Param("subjectTop") long subjectTop,
                                          @Param("subjectMid") long subjectMid,
                                          @Param("subjectBottom") long subjectBottom);

实体

@Entity
public class FacultySubject {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    private long facultyId;

    private int subjectId;

    public FacultySubject(long facultyId, int subjectId) {
        this.facultyId = facultyId;
        this.subjectId = subjectId;
    }

    public FacultySubject() {
    }
... getters and setters

string formatting documentation

1 个答案:

答案 0 :(得分:1)

@Query(value = "select * from faculty_subject where subject_id in(?1 , ?2 , ?3) group by faculty_id having count(*)==3", nativeQuery =  true)
List<FacultySubject> getFacultyBySubjects(long subjectTop, long subjectMid, long subjectBottom);