Sequelize:多个表的或条件 [英] Sequelize: Or-condition over multiple tables

查看:138
本文介绍了Sequelize:多个表的或条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 sequelizejs 在多个表上添加一个 or 条件.我的问题是我不知道如何在多个表上使用 or 运算符($or 和 Sequelize.or).

I want to add an or condition over multiple tables with sequelizejs. My problem is that I don't know how to use the or operators ($or and Sequelize.or) over more than one table.

假设我想实现以下 sql 查询:

Let's say I want to implement the following sql-query:

select * from A as a, B as b, C as c where (A.b_id = b.id and b.x = 7) or (A.c_id = C.id and c.z = "test")

我只会像这样用 sequelize 实现第一个条件:

I would implement only the first condition with sequelize like this:

A.findAll({
   include: [{ model: B, where: { x: 7 } }]
})

只有第二个条件是这样的:

And only the second condition like this:

A.findAll({
   include: [{ model: C, where: { z: "test" } }]
})

但是如何将两个查询组合成我想要的查询?

But how do I combine both queries into the one I want?

推荐答案

你可以这样做

A.findAll({
   include: [{model: B},{model: C}], 
   where: {
     '$or':{
        '$b.x$': 7, 
        '$c.z$': "test"
     }
   });

这篇关于Sequelize:多个表的或条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆