【问题标题】:HQL not equal and not in Query is not workingHQL 不相等且不在查询中不起作用
【发布时间】:2020-02-28 12:31:44
【问题描述】:
@Query("SELECT i FROM ICD10CM i WHERE i.type NOT IN ('C') AND i.name LIKE %:icdString% OR i.code LIKE %:icdString% OR i.desc LIKE %:icdString%")
List<ICD10CM> getICD10CMBySearch(@Param("icdString") String icdString);
我得到ICD10CM 的列表以及Type C,我想要Type C 以外的列表。
我试过not in ('C'),i.type != 'C',试过<>,也试过NOT LIKE C%。
我想要 HQL 查询而不是本机查询
【问题讨论】:
标签:
mysql
spring-boot
spring-data-jpa
hql
【解决方案1】:
因为你使用or:
OR i.code LIKE %:icdString% OR i.desc LIKE %:icdString%
将or 更改为and
【解决方案2】:
尝试使用 concat 构建适当的类似条件
并使用 () 构建适当的 OR 条件
@Query("SELECT i
FROM ICD10CM i
WHERE i.type NOT IN ('C')
AND (
i.name LIKE concat('%',:icdString,'%')
OR i.code LIKE concat('%',:icdString,'%')
OR i.desc LIKE concat('%',:icdString,'%')
)")
List<ICD10CM> getICD10CMBySearch(@Param("icdString") String icdString);