【问题标题】:Sequelize: How to use AND operator in WHERE on the same column?Sequelize:如何在同一列的 WHERE 中使用 AND 运算符?
【发布时间】:2020-04-28 02:22:32
【问题描述】:

我有这个问题:

model.findAndCountAll({
  attributes: ['FirstName', 'LastName'],
  include: [{
    model: models['hobbies'],
    as: 'Hobbies',
    attributes: ['ID', 'Name'],
    where: {
      [Sequelize.Op.and]: [
        { 'ID': 1 },
        { 'ID': 2 }
      ]
    }
  }]
})

我试图让一名员工具有查询中指定的两个爱好,尽管它返回 0 行。 AND 运算符在应用于同一列时似乎不起作用。有没有办法处理这种查询?

员工表数据:

 EmployeeID | FirstName | LastName | HobbyID
------------+-----------+----------+----------
          1 | John      | Doe      | 1
          1 | John      | Doe      | 1
          2 | Jane      | Doe      | 1

员工表关联:

model.hasMany(models['Hobbies'], { as: 'Hobbies', foreignKey: 'EmployeeID', targetKey: 'EmployeeID' });

输出:

{
  count: 0,
  rows: []
}

预期输出:

{
  FirstName: "John",
  LastName: "Doe",
  Hobbies: [
    { Name: "Sports - Basketball" },
    { Name: "Sports - Volleyball" }
  ]
}

并且它应该始终返回具有指定两种爱好的员工。

【问题讨论】:

  • 实际输出和预期输出是多少?

标签: node.js sequelize.js and-operator


【解决方案1】:

你可以试试这个

model.findAndCountAll({
  attributes: ['FirstName', 'LastName'],
  include: [{
    model: models['hobbies'],
    as: 'Hobbies',
    attributes: ['ID', 'Name'],
    where: {
        ID: [1,2]
      ]
    }
  }]
})

【讨论】:

    猜你喜欢
    • 2015-04-28
    • 2019-09-28
    • 2014-08-21
    • 2020-03-06
    • 2016-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-21
    相关资源
    最近更新 更多