gpt4 book ai didi

java - 如何在hql上使用 'group by'?

转载 作者:行者123 更新时间:2023-12-01 04:50:40 25 4
gpt4 key购买 nike

我有一个 native 查询,需要更改为 HQL。

原始查询是:

SELECT COUNT(DISTINCT `table`.`id`) FROM `database`.`table` WHERE
(`table`.`date`" " BETWEEN '" + year + "-01-01 00:00:00' AND '" + year
+ "-12-31 23:59:59') AND `table`.`box` NOT LIKE '' GROUP BY MONTH(`table`.`date`)";

我尝试过类似的方法:

StringBuilder hql = new StringBuilder(); 
hql.append(" select count(distinct table.id)");
hql.append(" from Table table");
hql.append(" where table.date between '?-01-01 00:00:00' and '?-12-31 23:59:59'");
hql.append(" and table.box not like ''");
hql.append("group by month (table.date)");
query.setParameter(1, Integer.toString(year));
query.setParameter(2, Integer.toString(year));

其中年份是作为参数传递给方法的 int。

生成的查询是:

Hibernate: select count(distinct table0_.id) as col_0_0_ from table table0_ where (table0_.date between '2013-01-01 00:00:00' and '2013-12-31 23:59:59') and (table0_.box not like '') group by month(table0_.date)

我的问题是:使用 native 查询,我得到一个值,而使用 hql,我得到第 2 个月(二月)的另一个值。第 1 个月(一月)的结果是相同的。

我在这里缺少什么?

提前致谢,

格鲁德维格

最佳答案

对我来说,它们似乎是相同的查询,但没有模式限定。您不是在不同的实例中运行它们吗?就像,一个在“数据库”中,另一个指向另一个加载了类似数据的库?

关于java - 如何在hql上使用 'group by'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15025687/

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