假设您在 CUSTOMER_MASTER 和 FOLLOWUP_GUIDES 之间有外键
即假设
CUSTOMER_MASTER.FOLL_UP_NUM is FK to FOLLOWUP_GUIDES.COMMNUMBER
CUSTOMER_MASTER.FOLL_UP_TYPE is FK to FOLLOWUP_GUIDES.COMMTYPE
然后你的查询
SELECT *
FROM
CUSTOMER_MASTER t0,
(SELECT t1.COMMNUMBER, t1.COMMTYPE FROM FOLLOWUP_GUIDES t1
WHERE (t1.STAGE_ID = '5')) t2
WHERE (t0.FOLL_UP_NUM = t2.COMMNUMBER
and T0.FOLL_UP_TYPE=t2.COMMTYPE AND (t0.COMPANY_ID = 'C001'))
相当于
SELECT t0.*
FROM CUSTOMER_MASTER t0 JOIN FOLLOWUP_GUIDES t1
WHERE t0.COMPANY_ID = 'C001'
AND t1.STAGE_ID = '5'
这可以在 JPA 中很容易地完成,并且看起来与此 SQL 非常相似(仅使用对象名称和字段名称而不是表名称和属性名称)。
JPA JPQL 查询将如下所示:
SELECT t0
FROM CustomerMaster t0 JOIN FollowupGuides t1
WHERE t0.companyId = "C001"
AND t1.stageId = "5"
也可以使用 JPA 子查询:
SELECT t0
FROM CustomerMaster t0
WHERE t0.companyId = "C001"
AND EXISTS (SELECT 1
FROM FollowupGuides t1
WHERE t1.commnumber = t0.follUpNum
AND t1.commtype = t0.FollUpType
AND t1.stageId = "5")
=B)