add=value在使用executequery的left join子句中

ohtdti5x  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(443)

使用Grails3.3.8和MySQLDB5.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

现在我的问题是如何在gorm中把“and field=value”放在左连接旁边?

li9yvcax

li9yvcax1#

可以使用with子句实现以下目的:

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

另请参阅:当前hibernate用户指南:hql explicit join

相关问题