gpt4 book ai didi

java - 如何查询日期范围内的数据库对象?

转载 作者:行者123 更新时间:2023-11-29 13:02:20 29 4
gpt4 key购买 nike

我想从数据库中返回所有对象,其中对象的两个 Date 字段都必须在特定时间段内。

对于两个日期字段,我能否比使用 BETWEEN 编写查询更好?

@Entity
public class Book {
Date from;
Date to;
}

//the period to search for
Date fromDate;
Date toDate;

//return all Book objects having from+to paramter laying within the period
b.from BETWEEN :fromDate AND :toDate AND b.to BETWEEN :fromDate AND :toDate

最佳答案

我不明白你真正需要什么,但如果我理解正确的话,你只需要检查日期边界。

 select * from books where b.from >= :fromDate AND b.to <= :toDate

您可以假设 fromDate 小于 toDate。

关于java - 如何查询日期范围内的数据库对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25869771/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com