Sequelize - 加入多列 [英] Sequelize - Join with multiple column

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

问题描述

我喜欢将以下查询转换为续集代码

I like to convert the following query into sequelize code

select * from table_a 
inner join table_b 
on table_a.column_1 = table_b.column_1
and table_a.column_2 = table_b.column_2

我尝试了许多方法并遵循了许多提供的解决方案,但我无法从 sequelize 代码实现所需的查询.

I have tried many approaches and followed many provided solution but I am unable to achieve the desired query from sequelize code.

我达到的最大值如下:

select * from table_a 
inner join table_b 
on table_a.column_1 = table_b.column_1

我也想要第二个条件.

and table_a.column_2 = table_b.column_2

有什么合适的方法来实现它吗?

any proper way to achieve it?

推荐答案

您需要定义自己的 JOIN 语句的 on 子句

You need to define your own on clause of the JOIN statement

ModelA.findAll({
    include: [
        {
            model: ModelB,
            on: {
                col1: sequelize.where(sequelize.col("ModelA.col1"), "=", sequelize.col("ModelB.col1")),
                col2: sequelize.where(sequelize.col("ModelA.col2"), "=", sequelize.col("ModelB.col2"))
            },
            attributes: [] // empty array means that no column from ModelB will be returned
        }
    ]
}).then((modelAInstances) => {
    // result...
});

这篇关于Sequelize - 加入多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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