我今天问了一个关于GORM的早期问题:How to fetch records in grails by max date and group at same time但是,有人建议使用HQL可以轻松实现。但是使用HQL我得到unexpected token
错误。当我研究这个时,我发现HQL不允许INNER JOINS
,除非两个实体之间存在关联:HQL, left join on the same table
所以,我迷路了。首先,我很沮丧为什么GORM不支持这样一个简单的查询,现在使用HQL我的问题是:如何在子集上执行INNER JOIN?
我尝试过的事情:
意外令牌:(靠近第1行,第16栏[选择c来自(选择 name,max(dateCreated)为com.mine.Color中的maxTime 按名称分组)作为内部联接颜色c在c.name = t.name和 c.dateCreated = t.maxTime]
我怀疑没有检测到Color
的第二个实例,因为包名并没有自动加上前缀。所以读了我试过的其他答案:
意外令牌:(靠近第1行,第16栏[选择c来自(选择 name,max(dateCreated)为com.mine.Color中的maxTime 按名称分组)t,com.mine.Color为c.name =上的c t.name和c.dateCreated = t.maxTime]
答案 0 :(得分:2)
你走了:
Color.executeQuery("""
Select c
From Color c
where c.dateCreated = (select max(b.dateCreated) from Color b where b.name = c.name)
""")