How do I do a JPQL SubQuery?

后端 未结 1 845
死守一世寂寞
死守一世寂寞 2020-12-28 14:15

It is possible to do the equivalent of this sql query in JPQL?

SELECT * 
 FROM COUNTRIES c WHERE COUNTRY_ID IN (
  SELECT DISTINCT COUNTRY_ID 
   FROM PORTS          


        
相关标签:
1条回答
  • 2020-12-28 14:26

    You need to test it with IN and subquery since both do work in JPQL (according to syntax reference they do work together). You may also look at MEMBER OF expressions.

    But there is a better approach in my opinion. Such queries are called correlated sub-queries and one can always re-write them using EXISTS:

    SELECT * FROM COUNTRIES c WHERE 
    EXISTS (
            SELECT 'found' FROM PORTS p 
            WHERE p.COUNTRY_ID = c.COUNTRY_ID AND STATE = 'A'
    ) 
    

    JPQL supports EXISTS with subqueries.

    0 讨论(0)
提交回复
热议问题