【发布时间】:2014-01-29 15:07:15
【问题描述】:
我想做一个非常简单的查询。
在 SQL 中是:
select * from table group by portfolio_id HAVING max(date)
在 grails 中我做了这样的事情:
def result = Table.executeQuery("from Table group by portfolio having max(date)")
但我得到了一个意外的 AST 节点
我不知道,我阅读了一些文档,但仍然无法得到它,为什么它不起作用。
这里有更详细的错误信息
| Error 2014-01-29 17:20:15,400 [http-bio-8080-exec-8] ERROR hql.PARSER - <AST>:1:97: unexpected AST node: max
| Error 2014-01-29 17:20:15,426 [http-bio-8080-exec-8] ERROR errors.GrailsExceptionResolver - QuerySyntaxException occurred when processing request: [GET] /Lumen/Controller unexpected AST node: max near line 1, column 97 [select portfolio.id,date from com.gwm.lumen.Table group by portfolio.id having max(date)].
堆栈跟踪如下:
Message: unexpected AST node: max near line 1, column 97 [select portfolio.id,date from com.gwm.lumen.Table group by portfolio.id having max(date)]
【问题讨论】:
-
如果您在数据库上尝试,该 SQL 查询对您有用吗?我会说不,因为它不是正确的组查询。像 select portfolio_id, max(date) from table group by portfolio_id HAVING max(date) 这样的东西应该可以工作
-
嗨,我向你保证它有效,我在发布之前尝试过。唯一的区别是在 SQL 查询中我没有做 gorm 可能会做的所有内连接。
-
有趣.. 我知道的任何数据库都会拒绝这样的查询。您使用哪个数据库?无论如何,即使您的数据库接受这样的查询,它也不是正确的 HQL,这就是 Grails 抛出错误的原因。尝试类似 Table.executeQuery("select portfolio, max(date) from Table group by portfolio with max(date)")
-
这是一个经典的MySql5.5。我试图在 executeQuery 中指定聚合和最大值的字段,但仍然是同样的问题
-
发布你得到的整个异常和一个 Table 类源
标签: hibernate grails hql grails-orm