【发布时间】:2017-01-05 05:48:32
【问题描述】:
我们想在查询中集成一个计算字段。
这个计算字段是用一个简单的SELECT MAX(x) FROM table2得到的。
但是在我们的主查询中,如果我们想过滤这个计算字段,我们必须将它集成到 where 子句中。
我们的问题是将子查询集成到WHERE...IN 条件中。
我们尝试使用 Subqueries.in 函数,但问题是我们无法注入值列表。
这里是我们想要的 SQL 请求。
select
this_.DMDE_CEE_ID as y0_,
this_.DT_DMDE as y1_,
(select
max(this0__.STATUT) as y0_
from
CS_FC_DMDE_CEE this0__
where
this0__.DMDE_CEE_ID=this_.DMDE_CEE_ID limit 1 ) as y16_
from
xxx this_
where
( select
max(controleDemandeCee_.STATUT) as y0_
from
CS_FC_DMDE_CEE controleDemandeCee_
where
controleDemandeCee_.DMDE_CEE_ID=this_.DMDE_CEE_ID
) IN (3,5)
order by
this_.NOM_DOS asc,
this_.DT_DMDE asc limit 10;
这是我们能得到的最好结果:
select
this_.DMDE_CEE_ID as y0_,
this_.DT_DMDE as y1_,
(select
max(this0__.STATUT) as y0_
from
CS_FC_DMDE_CEE this0__
where
this0__.DMDE_CEE_ID=this_.DMDE_CEE_ID limit 1 ) as y16_
from
xxx this_
where
5 IN ( select
max(controleDemandeCee_.STATUT) as y0_
from
CS_FC_DMDE_CEE controleDemandeCee_
where
controleDemandeCee_.DMDE_CEE_ID=this_.DMDE_CEE_ID
)
order by
this_.NOM_DOS asc,
this_.DT_DMDE asc limit 10;
带有条件和子查询的 Java 代码:
final DetachedCriteria dControleDemande = DetachedCriteria.forClass(ControleDemandeBean.class, CONTROLE);
dControleDemande.add(Restrictions.eqProperty(CONTROLE_DEMANDE_ID, DMD_ID));
dControleDemande.setProjection(Projections.max(CONTROLE + ".statut.id"));
cDmd.add(Subqueries.in(5L, dControleDemandeCee))
我们不能用长的列表/数组替换 5L。
有什么想法吗? 谢谢
nb:我们必须使用标准。客户端拒绝原生 HQL 或 SQL。
【问题讨论】:
标签: java hibernate subquery criteria