node.js - Sequelize:如何在同一列的 WHERE 中使用 AND 运算符?
问题描述
我有这个查询:
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" }
]
}
并且它应该始终返回具有指定的两种爱好的员工。
解决方案
你可以试试这个
model.findAndCountAll({
attributes: ['FirstName', 'LastName'],
include: [{
model: models['hobbies'],
as: 'Hobbies',
attributes: ['ID', 'Name'],
where: {
ID: [1,2]
]
}
}]
})
推荐阅读
- z3 - 如何使用循环或其他方法在 z3py 中自动调用类似 Or() 的函数?
- c++ - 有人可以告诉这个c ++代码有什么问题吗
- r - Read.table 没有正确解释表中的列数
- ios - """ 线程 1:异常:无法识别的选择器发送到实例 0x7fb12cc088c0" """ 异常错误
- spring-boot - EL1008E: 在“java.lang.String”类型的对象上找不到属性或字段“applicationName” - 可能不是公共的或无效的?
- oracle - 如何在两个字符串之间添加sql中的字符?
- c - 如何在这里打印我的结构的名称变量?
- ckeditor - CKEditor 4 忽略 uploadUrl 响应,仍然使用带有成功和错误消息的 blob URL
- google-visualization - 行而不是列中的 Google 可视化 API 系列数据。枢?
- android - 为什么 Firefox 移动地址栏在滚动时消失似乎对 100% 高度的 div 有影响?