【问题标题】:HQL in grails with having and max generate unexpected AST nodeGrails中的HQL具有和最大生成意外的AST节点
【发布时间】: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


【解决方案1】:

如果基础数据库是 MySQL,则不能在 having 子句中使用聚合函数 [max() 在这种情况下]。

SQL 函数和聚合函数允许在有和 如果底层数据库支持 order by 子句 (即,不在 MySQL 中)。

@摘自Hibernate Docs on group by clause。

我不确定最新的 MySQL 方言是否支持它。

【讨论】:

  • 非常感谢,这解释了很多。我刚刚将表单 5.5 升级到 5.6,但仍然是同样的问题。
猜你喜欢
  • 1970-01-01
  • 2013-09-28
  • 2014-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-26
  • 1970-01-01
  • 2019-08-23
相关资源
最近更新 更多