如何使用多对一解决 JPQL 查询中的重复行?
How to solve duplicated rows in the JPQL query with many-to-one?
我的查询几乎可以工作,但是它 returns 重复行,因为团队名称重复。我该如何解决这个问题?
我有:"team glass"
、"team foot"
和 "team swim"
。
TeamUserCompany
与 Team
具有多对一关系。
当我查询包含 "team"
的团队名称时,我得到了重复的行,因为它包含 "team glass"
、"team foot"
和 "team swim"
!
在前端我需要展示公司!
@Query("SELECT distinct teamUserComp FROM TeamUserCompany teamUserComp WHERE teamUserComp.team.name like %?1% ");
实体
public class TeamUserCompany extends AbstractEntity {
@ManyToOne(optional = false)
private Company company;
@ManyToOne
private User user;
@ManyToOne
private Team team;
}
如果你想要不同的公司,你必须 select 不同的公司而不是不同的 TeamUserCompany
:
SELECT distinct c
FROM TeamUserCompany tuc
JOIN tuc.company c
WHERE tuc.team.name like %?1%
我用了一个Group by 解决了这个问题。
@Query("SELECT distinct teamUserComp FROM TeamUserCompany teamUserComp WHERE teamUserComp.team.name like %?1% group by (teamUserInter.company)");
我的查询几乎可以工作,但是它 returns 重复行,因为团队名称重复。我该如何解决这个问题?
我有:"team glass"
、"team foot"
和 "team swim"
。
TeamUserCompany
与 Team
具有多对一关系。
当我查询包含 "team"
的团队名称时,我得到了重复的行,因为它包含 "team glass"
、"team foot"
和 "team swim"
!
在前端我需要展示公司!
@Query("SELECT distinct teamUserComp FROM TeamUserCompany teamUserComp WHERE teamUserComp.team.name like %?1% ");
实体
public class TeamUserCompany extends AbstractEntity {
@ManyToOne(optional = false)
private Company company;
@ManyToOne
private User user;
@ManyToOne
private Team team;
}
如果你想要不同的公司,你必须 select 不同的公司而不是不同的 TeamUserCompany
:
SELECT distinct c
FROM TeamUserCompany tuc
JOIN tuc.company c
WHERE tuc.team.name like %?1%
我用了一个Group by 解决了这个问题。
@Query("SELECT distinct teamUserComp FROM TeamUserCompany teamUserComp WHERE teamUserComp.team.name like %?1% group by (teamUserInter.company)");