3

我今天问了一个关于 GORM 的较早问题:How to fetch records in grails by max date and group 同时 然而,有人建议使用 HQL 可以轻松实现。但是使用 HQL 我得到了unexpected token错误。当我研究这个时,我发现 HQL 不允许INNER JOINS,除非两个实体之间存在关联:HQL,在同一个表上左连接

所以,我迷路了。首先,我很沮丧为什么 GORM 不支持这样一个简单的查询,现在有了 HQL,我的问题是:如何在子集上执行 INNER JOIN?

我试过的:

意外标记:(在第 1 行附近,第 16 列 [从(选择名称,max(dateCreated) as maxTime from com.mine.Color group by name 中选择 c)作为 t 内部连接颜色 c 在 c.name = t.name 和 c .dateCreated = t.maxTime ]

我怀疑Color没有检测到第二个实例,因为包名称没有自动添加到它的前缀。所以阅读我尝试的其他答案:

意外标记:(在第 1 行附近,第 16 列 [select c from (select name, max(dateCreated) as maxTime from com.mine.Color group by name ) as t ,com.mine.Color as c on c.name = t .name 和 c.dateCreated = t.maxTime ]

4

1 回答 1

2

干得好:

Color.executeQuery("""    
    Select c
    From Color c
    where c.dateCreated = (select max(b.dateCreated) from Color b where b.name = c.name)
    """)
于 2013-01-23T22:55:41.803 回答