如何添加“或"使用 sequilize 的 where 子句中的语句 [英] How to add "or" statement in the where clause using sequilize
本文介绍了如何添加“或"使用 sequilize 的 where 子句中的语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想为同一行创建多个 Where/OR 子句:这是我的请求正文
I want to make multiple Where / OR clauses for the same row: This is my request body
"sub_categories":[
{
"category_id":2
},
{
"category_id":1
}
]
这是我的javascript代码
This is my javascript code
var where = []
if (subcategories != undefined && subcategories.length) {
subcategories.forEach(async (item) => {
where.push({
'$subcategories.id$': item.id
})
});
}
预期产生的查询:
SELECT * FROM TABLE where ( sub_categories .category_id = 1 OR sub_categories .category_id = 2)
给我的问题:
SELECT * FROM TABLE where sub_categories .category_id = 2 AND sub_categories .category_id = 2)
我是否需要在代码中添加一些内容才能将 AND 转换为 OR?另外我还有一些其他可能的And"查询,所以我只想在这个查询上使用Or"
Do I need to add something to the code in order to transform that AND into a OR? Also I have some other possible "And" querys , so I just wanted to use the "Or" on this one
推荐答案
var where = {}
if (subcategories != undefined && subcategories.length) {
where['$or'] = []
subcategories.forEach((item) => {
where['$or'].push({
'$subcategories.id$': item.category_id
})
});
}
...
const result = await db.someModel.findAll({ where })
这篇关于如何添加“或"使用 sequilize 的 where 子句中的语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文