簡體   English   中英

當該字段是外鍵時,如何使用 OR 條件進行查詢

[英]how make a query in sequelize with a OR condition when that field is the foreignkey

我在續集中需要有關此查詢的一些幫助,此查詢完全按照我的意願工作,但我不知道如何執行該 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 是 null

我應該在這里做什么:

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- -代碼- )


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]
           }
         }
       }]
     }]
    })
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM