Sequelize 查询 - 比较两列中的日期
Sequelize query - compare dates in two columns
我在 Sequelize 中有一个带有两个日期列的 table - 即:
var Visit = sequelize.define("Visit", {
/*...*/
scheduleEndDate: {
type: DataTypes.DATE
}
actualEndDate: {
type: DataTypes.DATE
}
/*...*/
});
我想查询 returns 行,其中 actualEndDate 在 scheduleEndDate 之前 - 并且无法获得正确的格式。我为 findAll
查询的 where
部分尝试的是:
where: { actualEndDate: { lt: Visit.scheduleEndDate } }
- 抛出错误,因为 Visit 未定义(也尝试使用 this.scheduleEndDate - 也抛出错误)
where: { actualEndDate: '< scheduleEndDate' }
- 将 actualEndDate 与字符串“
我是否需要定义一个实例方法来进行日期比较/如何最好地解决?
这应该可以用
where: { actualEndDate: { $lt: sequelize.col('scheduleEndDate') } }
在最新版本 (v5) 中,您需要使用 Sequelize.Op
。 Docs
const Op = Sequelize.Op;
...
where: {
actualEndDate: {
[Op.lt]: sequelize.col('scheduleEndDate')
}
}
所有操作:
Project.findAll({
where: {
id: {
[Op.and]: {a: 5}, // AND (a = 5)
[Op.or]: [{a: 5}, {a: 6}], // (a = 5 OR a = 6)
[Op.gt]: 6, // id > 6
[Op.gte]: 6, // id >= 6
[Op.lt]: 10, // id < 10
[Op.lte]: 10, // id <= 10
[Op.ne]: 20, // id != 20
[Op.between]: [6, 10], // BETWEEN 6 AND 10
[Op.notBetween]: [11, 15], // NOT BETWEEN 11 AND 15
[Op.in]: [1, 2], // IN [1, 2]
[Op.notIn]: [1, 2], // NOT IN [1, 2]
[Op.like]: '%hat', // LIKE '%hat'
[Op.notLike]: '%hat', // NOT LIKE '%hat'
[Op.iLike]: '%hat', // ILIKE '%hat' (case insensitive) (PG only)
[Op.notILike]: '%hat', // NOT ILIKE '%hat' (PG only)
[Op.overlap]: [1, 2], // && [1, 2] (PG array overlap operator)
[Op.contains]: [1, 2], // @> [1, 2] (PG array contains operator)
[Op.contained]: [1, 2], // <@ [1, 2] (PG array contained by operator)
[Op.any]: [2,3] // ANY ARRAY[2, 3]::INTEGER (PG only)
},
status: {
[Op.not]: false // status NOT FALSE
}
}
})
我在 Sequelize 中有一个带有两个日期列的 table - 即:
var Visit = sequelize.define("Visit", {
/*...*/
scheduleEndDate: {
type: DataTypes.DATE
}
actualEndDate: {
type: DataTypes.DATE
}
/*...*/
});
我想查询 returns 行,其中 actualEndDate 在 scheduleEndDate 之前 - 并且无法获得正确的格式。我为 findAll
查询的 where
部分尝试的是:
where: { actualEndDate: { lt: Visit.scheduleEndDate } }
- 抛出错误,因为 Visit 未定义(也尝试使用 this.scheduleEndDate - 也抛出错误)
where: { actualEndDate: '< scheduleEndDate' }
- 将 actualEndDate 与字符串“ 我是否需要定义一个实例方法来进行日期比较/如何最好地解决?
这应该可以用
where: { actualEndDate: { $lt: sequelize.col('scheduleEndDate') } }
在最新版本 (v5) 中,您需要使用 Sequelize.Op
。 Docs
const Op = Sequelize.Op;
...
where: {
actualEndDate: {
[Op.lt]: sequelize.col('scheduleEndDate')
}
}
所有操作:
Project.findAll({
where: {
id: {
[Op.and]: {a: 5}, // AND (a = 5)
[Op.or]: [{a: 5}, {a: 6}], // (a = 5 OR a = 6)
[Op.gt]: 6, // id > 6
[Op.gte]: 6, // id >= 6
[Op.lt]: 10, // id < 10
[Op.lte]: 10, // id <= 10
[Op.ne]: 20, // id != 20
[Op.between]: [6, 10], // BETWEEN 6 AND 10
[Op.notBetween]: [11, 15], // NOT BETWEEN 11 AND 15
[Op.in]: [1, 2], // IN [1, 2]
[Op.notIn]: [1, 2], // NOT IN [1, 2]
[Op.like]: '%hat', // LIKE '%hat'
[Op.notLike]: '%hat', // NOT LIKE '%hat'
[Op.iLike]: '%hat', // ILIKE '%hat' (case insensitive) (PG only)
[Op.notILike]: '%hat', // NOT ILIKE '%hat' (PG only)
[Op.overlap]: [1, 2], // && [1, 2] (PG array overlap operator)
[Op.contains]: [1, 2], // @> [1, 2] (PG array contains operator)
[Op.contained]: [1, 2], // <@ [1, 2] (PG array contained by operator)
[Op.any]: [2,3] // ANY ARRAY[2, 3]::INTEGER (PG only)
},
status: {
[Op.not]: false // status NOT FALSE
}
}
})