gpt4 book ai didi

mysql - 如何从 sql 查询中获取 jpql 查询?

转载 作者:行者123 更新时间:2023-11-30 22:57:32 26 4
gpt4 key购买 nike

我可以像这样在 JPQL 中进行简单的 sql 查询,并且效果很好:

Query query = em.createQuery("SELECT p2 FROM Zp01 p2 where p2.gestionnaire IN (SELECT d.libelle FROM Affaire d)") ;
liszp01general= (List<Zp01>) query.getResultList();

但我无法将此查询转换为已经在 sql 中运行的 JPQL:

SELECT p2.* from zp01 p2 join (SELECT TYPEC,count(TYPEC) as cnt_typec FROM planning_cuisson group by TYPEC HAVING COUNT(TYPEC) > 0) p1 where p2.type_cuisson=p1.typec order by cnt_typec asc ;

我试过了但是没用:

Query query = em.createQuery("SELECT p2 FROM Zp01 p2 join ( select G.TYPEC,count(G.TYPEC) as cnt_typec from PlanningCuisson G group by G.TYPEC HAVING COUNT(G.TYPEC) > 0) p1 Where p2.typeCuisson=p1.typec and p2.ordre NOT IN (SELECT k.numof FROM OfSemiplanifie k) AND p2.gestionnaire IN (SELECT d.libelle FROM Affaire d) order by cnt_typec asc");
liszp01general= (List<Zp01>) query.getResultList();

最佳答案

这是一篇较旧的帖子,但如果它对其他人有所帮助,我将补充一点,当前版本的 JPQL 不支持 FROM 子句中的子查询。来自 JPQL 引用:

Subqueries are restricted to the WHERE and HAVING clauses in this release. Support for subqueries in the FROM clause will be considered in a later release of the specification.

当我不得不在一个项目上做类似的事情时,我不得不将聚合子查询向下移动到 where 子句中,向实体添加一个 Comparator,然后在 JPQL 调用之后执行 Collections.sort。所以查询将变成如下内容:

...
WHERE p2.typeCuisson IN (SELECT G.TYPEC
FROM PlanningCuisson G
GROUP BY G.TYPEC
HAVING COUNT(G.TYPEC) > 0)...

query.getResultList() 之后使用 Collections.sort

关于mysql - 如何从 sql 查询中获取 jpql 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25623376/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com