Add=使用execute查询的左联接子句中的值



使用grails 3.3.8和Mysql DB 5.5.x版本。我做了这个查询:

String query = $/
select new Map( i1.id as id,i1.name as name)
from CustomerComposition as c1
inner join c1.instrument as i1
left join i1.analisys as a1,
Instrument as i2
left join i2.analisys as a2,
Instrument as i3
left join i3.analisys as a3
where 
i1.id=i2.id and i1.id=i3.id and
c1.portfolio.id=:ptfId and a1.phase.id=:p1Id and a2.phase.id=:p2Id and a3.phase.id=:p3Id 
/$
List composition = CustomerComposition.executeQuery(query,
[ptfId: ptfId, p1Id: phase[0], p2Id: phase[1], p3Id: phase[2]])

左联接不起作用。然后我意识到它不起作用,因为我在WHERE中放了一个子句。我用简单的SQL语句进行了双重检查,事实上,一旦将条件移出位置,它就可以工作了。一个简单的剪辑只是为了澄清,之前它没有起作用:

SELECT
instrument1_.id
FROM
customer_ptf_composition customerpt0_
INNER JOIN instrument instrument1_ ON customerpt0_.instrument_id = instrument1_.id
LEFT JOIN analisys analisys4_ ON instrument1_.id = analisys4_.instrument_id
WHERE
customerpt0_.portfolio_id =1216 
AND analisys4_.phase_id =111

和之后,由于左/右连接的工作方式,它的工作方式:

SELECT
instrument1_.id
FROM
customer_ptf_composition customerpt0_
INNER JOIN instrument instrument1_ ON customerpt0_.instrument_id = instrument1_.id
LEFT JOIN analisys analisys4_ ON instrument1_.id = analisys4_.instrument_id AND analisys4_.phase_id =111 
WHERE
customerpt0_.portfolio_id =1216

现在我的问题是,如何将"and field=value"放在GORM中左边的join旁边?

您可以使用WITH子句来实现这一点:

left join i3.analisys a3 WITH a3.something = :someValue

另请参阅:当前Hibernate用户指南:HQL显式加入

相关内容

  • 没有找到相关文章

最新更新