【问题标题】:is it possible to use between , current_date and subdate functions in hql?是否可以在 hql 中使用 between 、 current_date 和 subdate 函数?
【发布时间】:2014-11-19 16:17:46
【问题描述】:

我想写这样的hql查询

<query name="user.by.date">
    FROM User as u
    where u.Date between subdate(now(), interval 3 day) and now()

如果错了怎么改? 我在哪里可以看到 hql 支持哪些功能? NOW() 和 CURRENT_DATE() 有什么区别? 是否可以在 NOW() 中使用 CURRENT_DATE()

【问题讨论】:

    标签: java sql hibernate jakarta-ee hql


    【解决方案1】:

    我建议不要在 HQL 中使用 subdate() 和 now() 等数据库特定函数,因为 HQL 用于使应用程序数据库独立。相反,您应该计算 subdate(now(), interval 3 day)now() 在 java 中并在此查询中将这两个值作为参数传递,这在 JAVA 中很容易做到,最后您的查询将如下所示。

    <query name="user.by.date">
        FROM User as u
        where u.Date between :prevdate and :currentdate</query>
    

    其中prevdate相当于subdate(now(),interval 3 day)currentdate相当于now( ),两者都是从JAVA代码中计算和传递的,这样会更容易,更独立于数据库。

    关于 now()current_date() 之间的区别,now 为您提供日期和时间,而 current_date 只给你日期作为输出。在mysql下面试试。

    现在():-->

    mysql> select now();
    +---------------------+
    | now()               |
    +---------------------+
    | 2014-11-19 22:38:15 |
    +---------------------+
    

    current_date() -->

    mysql> select current_date();
    +----------------+
    | current_date() |
    +----------------+
    | 2014-11-19     |
    +----------------+
    

    【讨论】:

      猜你喜欢
      • 2010-10-09
      • 2011-07-20
      • 1970-01-01
      • 2016-03-16
      • 2017-03-09
      • 2016-04-03
      • 2021-04-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多