node.js sequelize:多个“where”查询条件
如何查询具有多个条件的表? 以下是两个有效的示例:node.js sequelize:多个“where”查询条件,node.js,sequelize.js,Node.js,Sequelize.js,如何查询具有多个条件的表? 以下是两个有效的示例: Post.findAll({ where: ['deletedAt IS NULL'] }).success() 及 然后,如果我需要综合各种条件,我觉得我需要做一些事情,比如 Post.findAll({ where: [{topicId: req.params.id}, 'deletedAt IS NULL'] }).success() ,但它不起作用。 sequelize等待的语法是什么 节点调试说明: 调试:TypeError:对象
Post.findAll({ where: ['deletedAt IS NULL'] }).success()
及
然后,如果我需要综合各种条件,我觉得我需要做一些事情,比如
Post.findAll({ where: [{topicId: req.params.id}, 'deletedAt IS NULL'] }).success()
,但它不起作用。
sequelize等待的语法是什么
节点调试说明:
调试:TypeError:对象没有“替换”方法
如果重要的话…只要做:
Post.findAll(
{ where: ["topicId = ? AND deletedAt IS NULL", req.params.id] }
).success()
或者您需要或查询吗?您可以执行以下操作:
Post.findAll({ where: {deletedAt: null, topicId: req.params.id} })
将被转换为deletedAt的值为NULL
顺便说一句,如果需要deletedAt NOT为NULL,请使用:
请注意,截至本帖和sequelize的最新版本,上述答案已过时。我发布了我得到的解决方案,希望能为某人节省一些时间
filters["State"] = {$and: [filters["State"], {$not: this.filterSBM()}] };
请注意JSON对象中的$and数组以及缺少?代币替换
或者用一种更一般的方式,因为这可能有助于人们把自己的头绕在它周围:
{ $and: [{"Key1": "Value1"}, {"Key2": "Value2"}] }
在新版本中,试试这个
这给了我
从'tests'中选择'id','title','description','test','published','createdAt','updatedAt',作为'test',其中'test'。'title'类似于'%3%'或'test'。'description'类似于'%2%'
这正是我所需要的,你的解决方案完全符合我的需要,所以谢谢你。我必须说,你在那里所做的有点明显,我自己怎么能不这样做呢?但这更像是一种变通方法,我仍然很好奇,如果两种条件都有参数,我会怎么做?或者如果有更多的条件?Sequelize手册并没有关于这种情况的任何内容……Nvm,我刚刚发现,随后可以为“?”提供几个参数。再次感谢。请更新您的答案以检查多个where,show只显示一个where条件,因为他需要检查number和null。与其提及sequelize的最新版本,不如引用准确的版本。当我在2020年读到你2015年的帖子时,我相当肯定你引用的版本不再是最新的版本了。我在5年前的工作中也即兴写下了这一点,但我会记住这一点。
filters["State"] = {$and: [filters["State"], {$not: this.filterSBM()}] };
{ $and: [{"Key1": "Value1"}, {"Key2": "Value2"}] }
Post.findAll({
where: {
authorId: 12,
status: 'active'
}
}).then(function (data) {
res.status(200).json(data)
})
.catch(function (error) {
res.status(500).json(error)
});;
exports.findAll = (req, res) => {
const title = req.query.title;
const description = req.query.description;
Tutorial.findAll({
where: {
[Op.or]: [{
title: {
[Op.like]: `%${title}%`
}
},
{
description: {
[Op.like]: `%${description}%`
}
}
]
}
})
.then(data => {
res.send(data);
})
.catch(err => {
res.status(500).send({
message: err.message || "Some error occurred while retrieving tutorials."
});
});
};