使用HQL/SQL查询计算日期差异(以分钟为单位)
我的 MySQL 数据库中有一个时间时间戳字段,它映射到我的 Bean 中的日期数据类型。
现在我想要一个查询,通过它我可以获取数据库中当前时间戳与数据库中存储的时间戳之间的差异> 的所有记录。 20分钟。
我该怎么做呢?
我想要的是
select T from MyTab T where T.runTime - currentTime > 20 minutes
我尝试了以下
Query query = getSession().createQuery("Select :nw - T.runTime from MyTab T");
query.setDate("nw", new Date());
它返回一些值,例如 -2.0110930179433E13 。
我如何在 SQL/Hibernate 中对此进行建模? 即使我得到一些本机 SQL 查询(MySQL)也没关系。
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.
我想这个链接可能有帮助:
https://forum.hibernate.org/viewtopic.php ?p=2393545
这个也非常有用:
Hibernate 方言 + datediff 函数
希望有帮助。
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.