2016-12-28 44 views
0

我得到了以下sequelize查询片段,其中我喜欢做的多notLike查询:多notLike不需额外工作

article 
    .findAndCountAll({ 
     where: { 
     NAME: { 
      $like: '%'+req.body.search+'%' 
     }, 
     ITEM_NUMBER: { 
       $notLike: 'MF%', 
       $notLike: 'OLS%', 
       $notLike: 'MV%', 
       $notLike: 'MD%', 
       $notLike: 'AE%' 
     }, 
     PRICE: { 
      $gt: 0 
     }, 
     BLOCKED_FROM: { 
      $or: { 
      $gt: new Date(), 
      $eq: null 
      } 
     } 
     }, 
     limit: 20 
    }) 
    .then(function(result) { 
     res.json(result.rows); 
     //res.json(result.count); 
     console.log(result.count); 
     console.log(result.rows); 
    }); 

的问题是,对于地方部分ITEM_NUMBER仅接缝搭末notLike线($ notLike:'AE%')。由此产生的查询输出显示:

SELECT TOP 20 [NAME], [ITEM_NUMBER], [PRICE], [BLOCKED_FROM] FROM [ARTIKEL] AS [ARTIKEL] WHERE [ARTIKEL].[NAME] LIKE N'%testname%' AND [ARTIKEL].[ITEM_NUMBER] NOT LIKE N'AE%' AND [ARTIKEL].[PRICE] > 0 AND ([ARTIKEL].[BLOCKED_FROM] > N'2016-12-28 17:05:58.750' OR [ARTIKEL].[BLOCKED_FROM] IS NULL);

查询应该包含所有%notLike参数。任何想法有什么不对? THX 英戈

回答

0

试试:

... 
    ITEM_NUMBER: { 
     $or: [{ 
      $notLike: 'MF%' 
     }, { 
      $notLike: 'OLS%' 
     }, { 
      $notLike: 'MV%' 
     }, { 
      $notLike: 'MD%' 
     }, { 
      $notLike: 'AE%' 
     }] 
    }, 
    ... 
+0

这很有趣。有用。在操作员或操作员之后,我已经尝试过没有启动括号。我只拿了大括号,如[组合章节](http://docs.sequelizejs.com/en/v3/docs/querying/)中的文档所示,其中第一个或运算符示例没有方括号。 .Thx为此! – Inge

+0

很高兴帮助@Inge –

0

当使用阵列相同的密钥只需要之一,那么,请尝试更换这样的:

article 
.findAndCountAll({ 
    where: { 
    NAME: { 
     $like: '%'+req.body.search+'%' 
    }, 
    ITEM_NUMBER: { 
      $notLike: { $any: ['MF%','OLS%','MV%','MD%','AE%']} 
    }, 
    PRICE: { 
     $gt: 0 
    }, 
    BLOCKED_FROM: { 
     $or: { 
     $gt: new Date(), 
     $eq: null 
     } 
    } 
    }, 
    limit: 20 
}) 
.then(function(result) { 
    res.json(result.rows); 
    //res.json(result.count); 
    console.log(result.count); 
    console.log(result.rows); 
}); 

Operator of wherehttp://docs.sequelizejs.com/en/latest/docs/querying/?highlight= $ notLike

+0

我已经尝试过,但它然后会引发错误'未处理的拒绝SequelizeBaseError:关键字'ANY''附近的语法错误。任何想法? – Inge

+0

而我没有错ANY只适用于PG?我在mssql-server上。 – Inge