在Node应用程序中使用Knex.js与AND构造内部联接

时间:2019-05-08 17:41:24

标签: javascript sql node.js knex.js

是否有可能在SQL应用程序中使用knex.js编写类似于此Node的联接?

INNER JOIN member_staff ON members.id = member_staff.member_id AND staff.id = member_staff.staff_id

我认为它看起来像这样(尽管这不起作用,并且我找不到任何文档来解释如何做我想做的事):

.innerJoin('member_staff', 'members.id', 'member_staff.member_id').onAnd('staff.id', 'member_staff.staff_id')

2 个答案:

答案 0 :(得分:0)

您是说onIn吗?

.onIn('contacts.id', [7, 15, 23, 41])

OnAnd不存在。

您也可以使用onExists

knex.select('*').from('users').join('contacts', function() {
    this.on('users.id', '=', 'contacts.id').onExists(function() {
        this.select('*').from('accounts').whereRaw('users.account_id = accounts.id');
    })
})

答案 1 :(得分:0)

这就是我想要的:

.innerJoin('member_staff', function() {
    this.on('member.id', 'member_staff.member_id')
    .andOn('staff.id', 'member_staff.staff_id');
  })