Sequelize - Join with multiple column

后端 未结 2 624
梦如初夏
梦如初夏 2021-01-02 11:42

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.colum         


        
2条回答
  •  时光说笑
    2021-01-02 11:55

    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...
    });
    

提交回复
热议问题