简体   繁体   English

书架/ knex InnerJoin在多个表之间

[英]bookshelf/knex InnerJoin between multiple tables

I'm trying to do a join between three tables. 我正在尝试在三个表之间进行联接。

Let's call these tables, table a, b, and c. 让我们将这些表称为表a,b和c。

Table a has a primary key that is the target of a foreign key from b. 表a具有一个主键,该主键是b中外键的目标。 Similarly, Table a's primary key is the target of a foreign key from c. 同样,表a的主键是c的外键的目标。

The end query I want to do is something like 我想做的最终查询是这样的

SELECT * FROM a WHERE b.foreign_key = a.primary_key OR c.foreign_key = a.primary_key

My queryBuilder looks like this right now (starting from table a): 我的queryBuilder现在看起来像这样(从表a开始):

qb.innerJoin('b', 'b.foreign_key', 'a.primary_key') qb.innerJoin('c', 'c.foreign_key', 'a.primary_key')

I think this is wrong because when we do the join for table c, it's acting off of the previous join between a and b. 我认为这是错误的,因为当我们执行表c的联接时,它是根据a和b之间的先前联接执行的。

Is there a way I can do this three-way join where the joins are kind of added to each other/concatted together instead? 有什么方法可以执行这种三向联接,即联接之间是相互添加/连接在一起的吗?

Solved it. 解决了。

I just needed to use a left join instead. 我只需要使用左连接。 Using a left join would preserve my original table (table a), and prevents it from being culled down like what was happening when I joined a with b. 使用左联接将保留我的原始表(表a),并防止像当我将a与b联接时发生的那样将其剔除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM