我想将下面的查询转换为 sequelize 代码
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 代码实现所需的查询。
我达到的最大值如下:
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
有什么合适的方法可以实现吗?
最佳答案
您需要定义您自己的 JOIN
语句的 on
子句
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...
});
关于node.js - Sequelize - 加入多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42226351/