【问题标题】:Using inner join with more than one condition使用具有多个条件的内部联接
【发布时间】:2020-04-09 01:08:35
【问题描述】:

我有以下 SQL:

select table2.employee_id as manager_id, table1.employee_id as employee_id, table3.has_day_off
from "company".employee as table1
inner join "company".employee table2 on (table1.manager_id = table2.id)
inner join "company".employee_details table3 on (table3.employee_id = table1.employee_id and table3.manager_id = table2.employee_id and table3.branch_type = '<type>')
where table1.employee_code = '<employee code'

我将从给定的employeeCode 和branchType 返回employeeId、managerId 和“bool”dayOff。 我尝试了很多方法来将此逻辑传输到 Knex,在一个打字稿应用程序中,但无法开始工作。

任何人都可以为这个包含三个变量的 innerJoin 建议一种方法吗?

【问题讨论】:

标签: javascript sql knex.js


【解决方案1】:

请参阅 Mikael 的评论。我将通过一个示例来明确说明:

knex
  .withSchema('company')
  .select(
    't2.employee_id as manager_id',
    't1.employee_id as employee_id',
    't3.has_day_off'
  )
  .from('employee t1')
  .innerJoin('employee t2', 't1.manager_id', 't2.id')
  .innerJoin('employee_details t3', function () {
    this.on('t3.employee_id', '=', 't1.employee_id')
      .andOn('t3.manager_id', '=', 't2.employee_id')
      .andOn('t3.branch_type', '=', '<type>')
  })

当然没有经过测试,但这是一般的想法。

【讨论】:

    猜你喜欢
    • 2015-04-08
    • 2020-10-25
    • 1970-01-01
    • 1970-01-01
    • 2014-01-10
    • 2016-05-05
    • 2018-06-19
    • 1970-01-01
    • 2012-05-12
    相关资源
    最近更新 更多