[英]Sequelize - Query with Many-to-Many relationship using where and limit
[英]Sequelize many-to-many relationship: How to get result based on AND condition?
我希望有人能帮助我解决这个问题,或者如果我弄错了,可以提供替代方案。 我有两个表之间的多对多关系。
为简单起见,我们假设有Events
、 Users
、 EventUsers
,其中EventUsers
表具有eventId
、 userId
。
如何根据用户应明确为abc@test.com
和cde@test.com
的条件实现Events
表中的所有事件。
我尝试做这样的事情,但无济于事。
await Event.findAll({
include: [
{
model: User,
attributes: ['email'],
through: {
model: EventUsers,
attributes: [],
},
where: {
[Op.and]: [{ email: 'abc@test.com' }, { email: 'cde@test.com' }],
},
},
],
});
Event table
------------
id | title
------------
1 | lorem
2 | ipsum
User table
------------
id | email
------------
1 | abc@test.com
2 | cde@test.com
3 | efg@test.com
EventUsers table
------------
eventId| userId
------------
1 | 1
1 | 2
2 | 1
2 | 2
2 | 3
这必须给我一个 ID 为 1 的事件,其中电子邮件是abc@test.com
和cde@test.com
在 where 条件下尝试“Op.or”而不是“Op.and”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.