【问题标题】:How to get Distinct values with joined table?如何通过连接表获得不同的值?
【发布时间】:2023-04-04 12:55:01
【问题描述】:

我将按输入值进行过滤,但是当我执行此查询时,我得到了一些重复值!

    @Query("SELECT  DISTINCT inter FROM Intermediary inter WHERE inter.country=?1 and inter.isGood<>?2 " +
            "and inter.company.name like %?3% " +
            "or inter.company.identity like %?3% " +
            "or inter.company.client like %?3% " +
            "order by inter.createdAt DESC")
    Page<Intermediary> findAllByCompanyAndSearchValue(Country country, Integer isGood, String searchValue, Pageable pageable);

// Intermediary Entity 
{
   // ... Other attributes
  @ManyToOne(optional = false)
    private Company company;
}

// comapny Entity
{
    @OneToMany(mappedBy = "company")
    @JsonIgnore
    private List<Intermediary> intermediaries;

}

【问题讨论】:

    标签: sql database spring-boot spring-data-jpa hql


    【解决方案1】:

    尝试在“或”条件周围添加括号,以避免返回由满足任何可能的“和/或”组合的行产生的重复项:

    "or (inter.company.identity like %?3% " +
    "or inter.company.client like %?3% )" +
    

    【讨论】:

    • 这些括号出现语法错误!
    • @munirmounir 我编辑了我的示例代码以移动“或”命令后的第一个括号位置。
    • 非常感谢!我在and 之后移动了括号。 @Query("SELECT DISTINCT inter FROM Intermediary inter WHERE inter.country=?1 and inter.isGood&lt;&gt;?2 " + "and ( inter.company.name like %?3% " + "or inter.company.identity like %?3% " + "or inter.company.client like %?3% )" + "order by inter.createdAt DESC")
    猜你喜欢
    • 2017-05-22
    • 2019-08-19
    • 2016-12-29
    • 1970-01-01
    • 1970-01-01
    • 2018-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多