Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何使用$or和$and在sequelize查询中创建动态where子句_Sql_Node.js_Sequelize.js - Fatal编程技术网

Sql 如何使用$or和$and在sequelize查询中创建动态where子句

Sql 如何使用$or和$and在sequelize查询中创建动态where子句,sql,node.js,sequelize.js,Sql,Node.js,Sequelize.js,我正在尝试创建动态where子句来使用sequelize过滤数据。但是我在where子句中使用了“OR” 我尝试了下面给出的创建动态where子句的方法 let where = {}; if(obj.NoteCat) where.NoteCat = obj.NoteCat; if(obj.NoteSubCat) where.NoteSubCat = obj.NoteSubCat; where.NoteEntryDate = { [Op.and]: {

我正在尝试创建动态where子句来使用sequelize过滤数据。但是我在where子句中使用了“OR”

我尝试了下面给出的创建动态where子句的方法

let where = {};
if(obj.NoteCat)
   where.NoteCat = obj.NoteCat;
if(obj.NoteSubCat)
   where.NoteSubCat = obj.NoteSubCat;
where.NoteEntryDate = {
        [Op.and]: {
            [Op.gte]: obj.FromDate,
            [Op.lte]: obj.ToDate,
        }
};
where.NoteWeekEndingDate = {
        [Op.and]: {
            [Op.gte]: obj.FromDate,
            [Op.lte]: obj.ToDate,
        }
}; 
await table.findAll({
    where: where,
}
我希望下面给出我的疑问

SELECT *
FROM   table     
WHERE  NoteCat = ''
AND    NoteSubCat  = ''
OR     (NoteEntryDate      >= '2019-05-16 12:52:50.931' AND NoteEntryDate      <= '2019-05-16 12:52:50.931') 
OR     (NoteWeekEndingDate >= '2019-05-16 12:52:50.931' AND NoteWeekEndingDate <= '2019-05-16 12:52:50.931')
选择*
从桌子上
其中NoteCat=''
和NoteSubCat=''

或者(NoteEntryDate>='2019-05-16 12:52:50.931'和NoteEntryDate='2019-05-16 12:52:50.931'和NoteWeekEndingDate='2019-05-16 12:52:50.931'和NoteEntryDate='2019-05-16 12:52:50.931'和NoteWeekEndingDate您可以按相反的顺序创建它,如下所示:

let where = {
  [Op.or]: {
    NoteEntryDate: {
      [Op.and]: {
        [Op.gte]: obj.FromDate,
        [Op.lte]: obj.ToDate,
      }
    },
    NoteWeekEndingDate: {
      [Op.and]: {
        [Op.gte]: obj.FromDate,
        [Op.lte]: obj.ToDate,
      }
    }
  }
};
if(obj.NoteCat)
  where.NoteCat = obj.NoteCat;
if(obj.NoteSubCat)
  where.NoteSubCat = obj.NoteSubCat;
await table.findAll({
  where: where,
}

在顶级使用$或,如下所述:@ANNONAME我已经检查了上面的链接,但无法在动态where子句中进行调整。你能分享我的where子句的更新版本吗?我的朋友。有时我们没有跳出框框思考:)
let where = {
  [Op.or]: {
    NoteEntryDate: {
      [Op.and]: {
        [Op.gte]: obj.FromDate,
        [Op.lte]: obj.ToDate,
      }
    },
    NoteWeekEndingDate: {
      [Op.and]: {
        [Op.gte]: obj.FromDate,
        [Op.lte]: obj.ToDate,
      }
    }
  }
};
if(obj.NoteCat)
  where.NoteCat = obj.NoteCat;
if(obj.NoteSubCat)
  where.NoteSubCat = obj.NoteSubCat;
await table.findAll({
  where: where,
}