MySQL Sequelize Query 以查找落在日期范围内的所有记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43115151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sequelize Query to find all records that falls in between date range
提问by Anonymous Zombie
I have a model with columns:
我有一个带列的模型:
from: { type: Sequelize.DATE }
to: { type: Sequelize.DATE }
I want to query all records whose either from
OR to
falls in between the date ranges : [startDate, endDate]
我想查询其from
ORto
落在日期范围之间的所有记录:[startDate, endDate]
Tried Something Like:
尝试过类似的东西:
const where = {
$or: [{
from: {
$lte: startDate,
$gte: endDate,
},
to: {
$lte: startDate,
$gte: endDate,
},
}],
};
Something Like:
就像是:
SELECT * from MyTable WHERE (startDate <= from <= endDate) OR (startDate <= to <= endDate
回答by Akshay Pratap Singh
The solution which works for me is this:-
对我有用的解决方案是:-
# here startDate and endDate are Javascript Date object
const where = {
from: {
$between: [startDate, endDate]
}
};
For reference to know more about operators:- http://docs.sequelizejs.com/en/latest/docs/querying/#operators
参考了解更多关于运营商:- http://docs.sequelizejs.com/en/latest/docs/querying/#operators
Note:In MYSQLbetween
comparison operator is inclusive, which means it is equivalent to the expression (startDate <= from AND from <= endDate)
.
注意:在MYSQL 中between
比较运算符是inclusive,这意味着它等价于表达式(startDate <= from AND from <= endDate)
。
回答by M.A.K. Ripon
Try this condition what I think you are asking will do so.
试试这个条件,我认为你要问的会这样做。
For New Version Of Sequelize:
对于续集的新版本:
const where = {
[Op.or]: [{
from: {
[Op.between]: [startDate, endDate]
}
}, {
to: {
[Op.between]: [startDate, endDate]
}
}]
};
OR as your code structure:
或作为您的代码结构:
const where = {
$or: [{
from: {
$between: [startDate, endDate]
}
}, {
to: {
$between: [startDate, endDate]
}
}]
};
For more info you can follow this Sequelize official documents
有关更多信息,您可以关注此Sequelize 官方文档