我想通过所有应该匹配的列表.
@Query("select g.name as name FROM Gym g " +
"LEFT JOIN g.listofEquipment listofEquipment " +
"WHERE " +
"(((:listofEquipment) is null) or listofEquipment.id in (:listofEquipment)) "+
"AND (((:listofAmenity) is null) or listofAmenity.id in (:listofAmenity))")
Page<Map<String, Object>> listing(@Param("listofEquipment") Set<Integer> listofEquipment,@Param("listofAmenity") Set<Integer> listofAmenity)
以上查询工作为OR,但我需要AND
假设我通过1,2,3
那么结果应该是所有拥有设备1、2和3的体育馆
便利设施也一样
我试过了
Finding items with a set containing all elements of a given set with jpql
但它没有帮助我,因为有多个过滤器
网址为
http://localhost:8080/filter?listofEquipment=5,3&listofAmenity=51,13&sort=name
解决方法:
该查询应为您提供匹配所有listofEquipments的Gym.ids,并假定该列表不包含重复项.
select g.id as name
FROM Gym g
LEFT JOIN g.listofEquipment listofEquipment
WHERE listofEquipment.id in (:listofEquipment))
GROUP BY g.id
HAVING count(g.id) = #{#listofEquipment.size}
您可以将它和对listofAmenity的类似查询用作主查询中的子选择.
select g.name as name
FROM Gym g
LEFT JOIN g.listofEquipment listofEquipment
WHERE (((:listofEquipment) is null)
OR g.id in (
select g.id as name
FROM Gym g
LEFT JOIN g.listofEquipment listofEquipment
WHERE listofEquipment.id in (:listofEquipment))
GROUP BY g.id
HAVING count(g.id) = #{#listofEquipment.size}
))
AND (((:listofAmenity) is null)
OR g.id in (
select g.id as name
FROM Gym g
LEFT JOIN g.listofAmenity listofAmenity
WHERE listofAmenity.id in (:listofAmenity))
GROUP BY g.id
HAVING count(g.id) = #{#listofAmenity.size}
))