node.js - Sequelize - 加入多列

标签 node.js sequelize.js node-modules

我想将下面的查询转换为 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/

相关文章:

Javascript匿名函数向其添加对象

node.js - 如何通过 id 合并 Node.js 中的值数组?

node.js - 在模型的远程方法中使用 find 时的环回顺序过滤器,错误 :

javascript - 在sequelize where查询中使用OR语句

typescript - 如何从生产中的 npm 前端包中删除不需要的文件

javascript - NodeJS 的多个 module.exports

node.js - 找不到模块 'webpack/lib/web/FetchCompileWasmTemplatePlugin'

javascript - Node 应用程序,完全有效的 JSON, "d"怎么没有定义?

node.js - 为什么在Sequelize 中关联belongsTo 会给我这个错误?

postgresql - 如何通过查询 Sequelize 按顺序设置 collat​​e en_us.UTF-8