使用HQL / SQL查询以分钟计算日期差异 [英] Calulating date difference in minutes by using HQL/SQL query

查看:151
本文介绍了使用HQL / SQL查询以分钟计算日期差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的MySQL数据库中有一段时间Timestamp,它映射到我的Bean中的日期数据类型。

现在我想要一个查询,通过该查询我可以获取数据库中的所有记录,当前时间戳与存储在数据库中的时间戳之间的差异大于20分钟。



我该怎么做?



我想要的是

 从MyTab T中选择T,其中T.runTime  -  currentTime> 20分钟

我尝试了以下内容:

  Query query = getSession()。createQuery(Select:nw  -  MyTab T的T.runTime); 
query.setDate(nw,new Date());

它返回一些值,如-2.0110930179433E13。

如何在SQL / Hibernate中对此进行建模?
即使我得到了一些本地SQL查询(MySQL),也没关系。

解决方案

我想这个链接可能有帮助:



https:// forum.hibernate.org/viewtopic.php?p=2393545

这一个也很有帮助:



Hibernate Dialects + datediff功能



希望它有帮助。


I have a field of time Timestamp in my MySQL database which is mapped to a date datatype in my Bean.
Now I want a query by which I can fetch all records in the database for which the difference between the current timestamp and the one stored in the database is > 20 minutes.

How can I do it?

What I want is

select T from MyTab T where T.runTime - currentTime > 20 minutes

I tried the following

Query  query =  getSession().createQuery("Select :nw - T.runTime   from MyTab T");
  query.setDate("nw", new Date());

It returns some values like -2.0110930179433E13 .

How can I model this in SQL/Hibernate? Even if I get some native SQL query (MySQL) then it's ok.

解决方案

I guess this link might help:

https://forum.hibernate.org/viewtopic.php?p=2393545

and this one is also very helful:

Hibernate Dialects + datediff function

Hope it helps.

这篇关于使用HQL / SQL查询以分钟计算日期差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆