如何进行超过时间期限的查询?

时间:2018-12-06 15:02:42

标签: sql postgresql hibernate spring-boot

我有一个名称为 Task 的表,如下所示,该表的属性为 stopTime

@Entity
@Table(name = "task", schema = "public")
public class Task {

 @Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 private Long id;

 @NotEmpty
 private String date;

 @NotEmpty
 private String startTime;

 @NotEmpty
 private String stopTime;

 @NotEmpty
 @Column(length=1000)
 private String description;

 @ManyToOne
 @JoinColumn(name="USER_EMAIL")
 private User user;

 public Long getId() {
    return id;
 }

 public void setId(Long id) {
    this.id = id;
 }

 public String getDate() {
    return date;
 }

 public void setDate(String date) {
    this.date = date;
 }

 public String getStartTime() {
    return startTime;
 }

 public void setStartTime(String startTime) {
    this.startTime = startTime;
 }

 public String getStopTime() {
    return stopTime;
 }

 public void setStopTime(String stopTime) {
    this.stopTime = stopTime;
 }

 public String getDescription() {
    return description;
 }

 public void setDescription(String description) {
    this.description = description;
 }

 public User getUser() {
    return user;
 }

 public void setUser(User user) {
    this.user = user;
 }

现在,我要在 PostgreSQL 中进行查询,以选择所有已超过截止日期的任务,如下所示:

@Query("select stopTime from Task where ")
List<String> deleteAll();

。建议将任务的停止时间当前时间进行比较。请帮助我完成此查询。 预先感谢!

1 个答案:

答案 0 :(得分:0)

如果要比较日期时间类型的列,请使用INTERVALBETWEEN

select * from Task where stopTime BETWEEN (NOW() - INTERVAL '3 day') AND NOW()

出于删除目的,请尝试使用此select * from Task where stopTime < NOW()select * from Task where stopTime IS NOT NULL