【问题标题】:node.js sequelize: multiple 'where' query conditionsnode.js sequelize:多个“where”查询条件
【发布时间】:2012-06-04 04:26:49
【问题描述】:

如何查询具有多个条件的表? 以下是两个都有效的示例:

Post.findAll({ where: ['deletedAt IS NULL'] }).success()

和

Post.findAll({ where: {topicId: req.params.id} }).success()

然后,如果我需要结合条件,我觉得我需要做类似的事情

Post.findAll({ where: [{topicId: req.params.id}, 'deletedAt IS NULL'] }).success()

,但它不起作用。
sequelize 等待的语法是什么?

节点调试说:

DEBUG: TypeError: Object # has no method 'replace'

如果重要的话……

【问题讨论】:

    标签: node.js sequelize.js


    【解决方案1】:

    只要做:

    Post.findAll(
        { where: ["topicId = ? AND deletedAt IS NULL", req.params.id] }
    ).success()
    

    或者您是否需要 or 查询?

    【讨论】:

    • 这是我需要的,你的解决方案完全符合我的需要,所以谢谢。
    • 我必须说你在那里做了什么很明显,我自己怎么可能不这样做?)但感觉更像是解决方法,我仍然很好奇如果两个条件都有我会怎么做参数?或者如果有更多的条件? Sequelize 手册没有关于这种情况的任何内容...
    • Nvm,我刚刚发现可以为“?”提供几个参数。再次感谢)
    【解决方案2】:

    你可以这样做:

    Post.findAll({ where: {deletedAt: null, topicId: req.params.id} })
    

    会被翻译成deletedAt IS NULL

    顺便说一句,如果您需要deletedAt NOT IS NULL,请使用:

    Post.findAll({ where: {deletedAt: {$ne: null}, topicId: req.params.id} })
    

    【讨论】:

    • 请更新您的答案以检查多个位置,显示仅显示一个条件,因为他需要检查数字和空值。
    【解决方案3】:

    请注意,截至本帖和 2015 版续集,上述答案已过时。我正在发布我正在努力的解决方案,希望能节省一些时间。

    filters["State"] = {$and: [filters["State"], {$not: this.filterSBM()}] };
    

    注意 JSON 对象中的 $ 和数组,缺少 ?令牌替换。

    或者换一种更笼统的方式,因为这可能会帮助人们理解它:

    { $and: [{"Key1": "Value1"}, {"Key2": "Value2"}] }
    

    【讨论】:

    • 不要提及the most recent version of sequelize,如果您引用确切的版本将会很有用。当我在 2020 年阅读您 2015 年的帖子时,相当确定您引用的版本不再是 the most recent version。
    • 合理。我也在 5 年前的工作中即兴写了这句话,但我会牢记这一点。
    【解决方案4】:

    在新版本试试这个

    Post.findAll({
      where: {
        authorId: 12,
        status: 'active'
      }
    }).then(function (data) {
        res.status(200).json(data)
                })
       .catch(function (error) {
                    res.status(500).json(error)
       });;
    

    【讨论】:

      【解决方案5】:

      这给了我 SELECT `id`, `title`, `description`, `test`, `published`, `createdAt`, `updatedAt` FROM `tests` AS `test` WHERE (`test`.`title` LIKE '%3%' OR `test`.`description` LIKE '%2%');

      exports.findAll = (req, res) => {
          const title = req.query.title;
          const description = req.query.description;
          Tutorial.findAll({
                  where: {
                      [Op.or]: [{
                              title: {
                                  [Op.like]: `%${title}%`
                              }
                          },
                          {
                              description: {
                                  [Op.like]: `%${description}%`
                              }
                          }
                      ]
                  }
              })
              .then(data => {
                  res.send(data);
              })
              .catch(err => {
                  res.status(500).send({
                      message: err.message || "Some error occurred while retrieving tutorials."
                  });
              });
      
      };
      

      【讨论】:

      • 这是正确的答案。出于某种原因,上面的大多数人都提供了 AND 条件的答案。这适用于 Sequelize 6。
      猜你喜欢
      • 1970-01-01
      • 2018-05-06
      • 2013-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多