我有一个带列的模型:
from: { type: Sequelize.DATE }
to: { type: Sequelize.DATE }
并希望查询from
或to
落在日期范围之间的所有记录:[startDate, endDate]
我尝试过类似的事情:
const where = {
$or: [{
from: {
$lte: startDate,
$gte: endDate,
},
to: {
$lte: startDate,
$gte: endDate,
},
}],
};

像: SELECT *来自MyTable WHERE(startDate< = from< = endDate)OR(startDate< = to< = endDate
答案 0 :(得分:35)
对我有用的解决方案是: -
# here startDate and endDate are Javascript Date object
const where = {
from: {
$between: [startDate, endDate]
}
};
有关运营商的更多信息,请参阅: - http://docs.sequelizejs.com/en/latest/docs/querying/#operators
注意:强>
在 MYSQL between
比较运算符包含,这意味着它等同于表达式(startDate <= from AND from <= endDate)
。
答案 1 :(得分:1)
尝试这种情况,我想你会问的。
用于Sequelize的新版本:
-a Each name is an indexed array variable (see Arrays above).
-A Each name is an **associative** array variable (see Arrays above).
或作为您的代码结构:
const where = {
[Op.or]: [{
from: {
[Op.between]: [startDate, endDate]
}
}, {
to: {
[Op.between]: [startDate, endDate]
}
}]
};
有关更多信息,您可以关注此Sequelize official documents