【问题标题】:Hibernate query date compare on year and month only休眠查询日期仅比较年和月
【发布时间】:2012-10-10 00:51:18
【问题描述】:

我需要从具有birthDate 列的表中获取一些记录,该列是Date(没有时间)。我只需要通过比较年份和月份来过滤记录。 例如,我想要所有出生于 1990 年 3 月的用户。日期无关紧要。

你是怎么处理的?

【问题讨论】:

    标签: sql hibernate hql


    【解决方案1】:

    您可以使用from Entity where to_char(birthDate,'YYYY/MM') = '1990/03' 这适用于 HQL

    【讨论】:

    【解决方案2】:

    你可以试试这个

     select * from table where month(birthdate)=03 and year(birthdate)=1990
    

    【讨论】:

    • 这是 PL/SQL 吗?我正在寻找一种休眠解决方案(HQL),但如果找不到,我将不得不使用特定于数据库的解决方案。
    • 使用 month() 和 year() 函数的等价物是:“来自实体,其中 year(birthDate)=:year and month(birthDate)=:month”,然后绑定 :year和 :month 参数使用查询的 setString(yourDate's value)。
    【解决方案3】:

    这是一个适合我的 HQL 查询

    from Person as p where year(p.birthDate)=year(:bDate) and month(p.birthDate)=month(:bDate)"
    

    bDate的类型是Date,可以传Integer

    from Person as p where year(p.birthDate)=:bYear and month(p.birthDate)=:bMonth
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-06
      • 1970-01-01
      • 1970-01-01
      • 2011-09-10
      • 1970-01-01
      • 1970-01-01
      • 2010-09-18
      • 1970-01-01
      相关资源
      最近更新 更多