java如何仅从JPQL中的日期时间字段按日期分组
对于mysql,我编写了如下查询
SELECT * FROM mytable GROUP BY DATE(dateTimeField)
但是我不能在JPQL中使用DATE()
函数。
有人知道如何解决这个问题吗
你可以在下面搜索框中键入要查询的问题!
对于mysql,我编写了如下查询
SELECT * FROM mytable GROUP BY DATE(dateTimeField)
但是我不能在JPQL中使用DATE()
函数。
有人知道如何解决这个问题吗
# 1 楼答案
恐怕这超出了JPQL提供的范围。您将不得不求助于本机查询
以下是JPQL中可用函数的列表:
# 2 楼答案
如果在引擎盖下使用Hibernate,您可以尝试:
使用自定义函数创建自己的方言
在
persistence.xml
中注册您的方言在JPQL中使用您的函数
# 3 楼答案
如果使用EclipseLink,可以使用JPQL中的
FUNC()
函数调用特定的数据库函数:# 4 楼答案
对于Hibernate:
建议1:
使用
cast(dateTimeField as date)
。见here建议2:
您可以尝试连接
year
、month
和year
HQL expressions看看this testcase的第360行
但是花点时间看看生成的SQL,它可能(也可能)丑陋而缓慢