使用 HQL 的 varchar 列的 MAX

发布于 2024-12-10 16:44:01 字数 994 浏览 0 评论 0原文

我正在尝试使用 hibernate 查询语言查找 mySQL DB 中 varchar 列的最大值。

这是我正在尝试执行的查询,这在本机 SQL 中运行良好:

select max(cast(rs.marks as unsigned)) from Results rs;

其中 column:marks 是 varchar

假设如果列标记(所有值都保存为文本)具有以下行:

65
75
82
41

那么查询应该返回唯一的结果“82”。 但是当我在 HQL 中编写相同的内容时,它不起作用。谁能帮我告诉我如何在 Hibernate 中写这个?

控制台说:

java.lang.IllegalStateException: No data type for node:org.hibernate.hql.ast.tree.AggregateNode
[AGGREGATE] AggregateNode: 'max'
[METHOD_CALL] MethodNode: '('
[METHOD_NAME] IdentNode: 'cast' {originalText=cast}
[EXPR_LIST] SqlNode: 'exprList'
[DOT] DotNode: 'results0_.marks_'
{propertyName=marks,dereferenceType=4,propertyPath=marks,path={synthetic-alias}.marks,tableAlias=results0_,className=com.Results,classAlias=rs}
[IDENT] IdentNode: '{synthetic-alias}' {originalText={synthetic-alias}}
[IDENT] IdentNode: 'marks' {originalText=marks}
[IDENT] IdentNode: 'unsigned' {originalText=unsigned}

I am trying to find the maximum value of a varchar column in mySQL DB using hibernate query language.

This is the query i am trying to do, and this works fine in native SQL:

select max(cast(rs.marks as unsigned)) from Results rs;

where column:marks is a varchar.

Suppose if column marks (where all the values are saved as texts) has the following rows:

65
75
82
41

Then the query should return me a unique result "82".
But when i wrote the same in HQL, it doesn't work. Can anyone please help me out by telling me how to write this in Hibernate?

Console says:

java.lang.IllegalStateException: No data type for node:org.hibernate.hql.ast.tree.AggregateNode
[AGGREGATE] AggregateNode: 'max'
[METHOD_CALL] MethodNode: '('
[METHOD_NAME] IdentNode: 'cast' {originalText=cast}
[EXPR_LIST] SqlNode: 'exprList'
[DOT] DotNode: 'results0_.marks_'
{propertyName=marks,dereferenceType=4,propertyPath=marks,path={synthetic-alias}.marks,tableAlias=results0_,className=com.Results,classAlias=rs}
[IDENT] IdentNode: '{synthetic-alias}' {originalText={synthetic-alias}}
[IDENT] IdentNode: 'marks' {originalText=marks}
[IDENT] IdentNode: 'unsigned' {originalText=unsigned}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

∝单色的世界 2024-12-17 16:44:01

遇到这个问题并决定回答,尽管它已经很老了。

首先这足以获得最大值

SELECT MAX(marks) max_value FROM Results;

其次HSQL中没有UNSIGNED关键字。因此如果结果值需要转换为INT

SELECT CAST(MAX(marks) AS INT) max_value FROM Results;

Came across this question and decided to answer although it's very old.

First of all this will suffice to get MAX value

SELECT MAX(marks) max_value FROM Results;

Secondly there is no UNSIGNED keyword in HSQL. Therefore if the result value needs to be converted to INT

SELECT CAST(MAX(marks) AS INT) max_value FROM Results;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文