gpt4 book ai didi

mysql - 相当于 HQL 中的 GREATEST()

转载 作者:行者123 更新时间:2023-11-29 15:33:42 30 4
gpt4 key购买 nike

我正在尝试在 HQL 中找到两个日期中最好的一个。

在 SQL 中,这是有效的:

SELECT ( GREATEST(a1.startDate, '2019-10-01T00:00:00Z') ) FROM Activity AS a1 WHERE a1.id = 66232

但是 HQL 中的等效项给了我以下错误:

java.lang.IllegalArgumentException: org.hibernate.QueryException: No data type for node: org.hibernate.hql.internal.ast.tree.MethodNode 
\-[METHOD_CALL] MethodNode: '('
+-[METHOD_NAME] IdentNode: 'GREATEST' {originalText=GREATEST}
\-[EXPR_LIST] SqlNode: 'exprList'
+-[NAMED_PARAM] ParameterNode: '?' {name=param0, expectedType=null}
\-[NAMED_PARAM] ParameterNode: '?' {name=param1, expectedType=null}
[SELECT GREATEST(:param0,:param1) FROM xxx.xxx.xxx.Activity AS a1 WHERE a1.id = :param2]
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:133)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:157)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:164)
... 134 more

有没有办法通过 HQL 中的两个日期来实现与 MySQL 的 GREATEST() 相同的功能?

编辑:我也尝试使用 CASE WHEN ... THEN ... ELSE ... END 但最终出现以下错误:

java.lang.IllegalArgumentException: org.hibernate.QueryException: No data type for node: org.hibernate.hql.internal.ast.tree.SearchedCaseNode 
\-[CASE] SearchedCaseNode: 'CASE'
+-[WHEN] SqlNode: 'WHEN'
| +-[GT] BinaryLogicOperatorNode: '>'
| | +-[NAMED_PARAM] ParameterNode: '?' {name=param0, expectedType=null}
| | \-[NAMED_PARAM] ParameterNode: '?' {name=param1, expectedType=null}
| \-[NAMED_PARAM] ParameterNode: '?' {name=param2, expectedType=null}
\-[ELSE] SqlNode: 'ELSE'
\-[NAMED_PARAM] ParameterNode: '?' {name=param3, expectedType=null}
[SELECT CASE WHEN :param0 > :param1 THEN :param2 ELSE :param3 END FROM flowzone.model.activity.Activity AS a1 WHERE a1.id = :param4]
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:133)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:157)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:164)
... 138 more

最佳答案

MySQL手册将GREATEST()解释为

GREATEST(value1,value2,...)

With two or more arguments, returns the largest (maximum-valued) argument.

所以MySQL的GREATEST(value1,value2,...)函数/语法糖应该与下面的MySQL语法相同..

SELECT 
MAX(greatest.v)
FROM (
SELECT value1 AS v
UNION ALL
SELECT value2 AS v
[, UNION ALL SELECT ...]
) AS greatest

不确定它是否是 HQL 证明,但也许是下面的查询?

SELECT 
MAX(alias.startDate)
FROM (
SELECT a1.startDate FROM Activity AS a1 WHERE a1.id = 66232
UNION ALL
SELECT CAST('2019-10-01 00:00:00' AS DATETIME) AS startDate
) AS alias

关于mysql - 相当于 HQL 中的 GREATEST(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58469065/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com