【发布时间】:2019-04-03 09:29:04
【问题描述】:
使用 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 语句,确实一旦将条件移出 where 它就可以工作。一个简单的片段,只是为了澄清,之前没有用:
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 的左连接旁边?
【问题讨论】:
标签: java mysql hibernate grails grails-orm