gpt4 book ai didi

javascript - 当该字段是外键时,如何使用 OR 条件在 sequelize 中进行查询

转载 作者:行者123 更新时间:2023-12-03 22:32:04 24 4
gpt4 key购买 nike

我在 sequelize 中需要一些有关此查询的帮助,此查询完全按照我想要的方式工作,但我不知道如何执行该 OR 条件:

select * from 
core."Customers" c

inner join core."Townhouses" t
on t.id = c.townhouseid

inner join portal."TownhouseFreeFreights" tf
on tf.townhouseid = t.id
or tf.townhouseid is null

where c.id = 1
and tf."minValue" >= 0
我的问题是那部分:
或 tf.townhouseid 为空
我应该在这里做什么:
getFreightRules(){
return Customer.findAll({
include: [{
model: Townhouse,
require: true,
include:[{
model: TownhouseFreeFreights,
}]
}]

})
}

最佳答案

这样的事情应该可以工作( https://sequelize.org/master/manual/model-querying-basics.html#examples-with--code-op-and--code--and--code-op-or--code- )


getFreightRules(){
return Customer.findAll({
include: [{
model: Townhouse,
require: true,
include:[{
model: TownhouseFreeFreights,
where: {
[Op.or]: [
{ tf.townhouseid: t.id },
{ tf.townhouseid: null }
]
}
}]
}]

})
}

或者

getFreightRules(){
return Customer.findAll({
include: [{
model: Townhouse,
require: true,
include:[{
model: TownhouseFreeFreights,
where: {
tf.townhouseid: {
[Op.or]: [t.id, null]
}
}
}]
}]
})
}

关于javascript - 当该字段是外键时,如何使用 OR 条件在 sequelize 中进行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66052756/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com