Sequelize 查询 - 比较两列中的日期 [英] Sequelize query - compare dates in two columns

查看:121
本文介绍了Sequelize 查询 - 比较两列中的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Sequelize 中有一个带有两个日期列的表格 - 即:

I have a table in Sequelize with two date columns - i.e. with :

var Visit = sequelize.define("Visit", {
  /*...*/
  scheduleEndDate: {
    type: DataTypes.DATE
  }
  actualEndDate: {
    type: DataTypes.DATE
  }
  /*...*/
});

我想进行一个查询,该查询返回实际结束日期在 scheduleEndDate 之前的行 - 并且无法获得正确的格式.我为 findAll 查询的 where 部分尝试的是:

I want to make a query that returns rows where actualEndDate is before scheduleEndDate - and can't get the format right. What I've tried for the where part of my findAll query is:

where: { actualEndDate: { lt: Visit.scheduleEndDate } }

- 由于未定义访问而引发错误(也尝试使用 this.scheduleEndDate - 也引发错误)

- throws an error because Visit not defined (have also tried with this.scheduleEndDate - also throws an error)

where: { actualEndDate: '< scheduleEndDate' }

- 将 actualEndDate 与字符串 '< 进行字符串比较.scheduleEndDate'

- does a string comparison of actualEndDate against the string '< scheduleEndDate'

我是否需要定义一个实例方法来进行日期比较/如何最好地解决?

Do I need to define an instance method to do the date comparison / how best to solve?

推荐答案

这应该可以通过

where: { actualEndDate: { $lt: sequelize.col('scheduleEndDate') } }

这篇关于Sequelize 查询 - 比较两列中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆