简体   繁体   English

我如何内部联接多个表?

[英]How do I inner join multiple tables?

I have tables A, B and C and I want to get matching values for from all tables (tables have different columns). 我有表A,表B和表C,我想从所有表中获取匹配值(表具有不同的列)。

Table A (primary key = id)
+------+-------+
| id   | name  |
+------+-------+
| 1    | Ruby  |
| 2    | Java  |
| 3    | JRuby |
+------+-------+
Table B (pid is reference to A(id) - No primary key)
+------+------------+
| pid  | name       |
+------+------------+
| 1    | Table B    |
+------+------------+
Table C (primary key = id, pid is reference to A(id))
+------+------+------------+
| id   | pid  | name       |
+------+------+------------+
| 1    | 2    | Table C    |
+------+------+------------+

So my below query returned nothing. 所以我下面的查询什么也没返回。 Whats wrong here? 怎么了 Is it treated as AND when multiple inner joins present? 当存在多个内部联接时,是否将其视为AND?

Select A.* from A 
  inner join B ON a.id = b.pid
  inner join C ON a.id = c.pid;

As you first join 初次加入时

1 | Ruby | Table B

and then try to join Table C , there is no match for pid 2 in the aforementioned result, the result is therefore empty. 然后尝试加入Table C ,上述结果中没有pid 2匹配项,因此结果为空。

An inner join excludes everything that doesn't match. 内部联接会排除所有不匹配的内容。 So after you joined against B, you were left with only one record (id=1). 因此,在与B对抗后,您只剩下一个记录(id = 1)。 Your inner join against C doesn't have any matches from what's left, so you get nothing. 您对C的内部联接没有剩余的匹配项,因此您一无所获。

I suppose a union would do the trick: 我想union可以解决这个问题:

select A.* from A join B on a.id = b.pid
union
select A.* from A join C on a.id = c.pid

Or there are other ways, like where a.id in (select pid from b) or a.id in (select pid from c) 或还有其他方法,例如where a.id in (select pid from b) or a.id in (select pid from c)

When you inner-join like this, a single row from A needs to exist such that a.id = b.pid AND a.id = c.pid are true at the same time. 当您像这样进行内部a.id = b.pid ,需要存在A一行,以便a.id = b.pida.id = c.pid为true。 If you examine the rows in your examples, you would find that there is a row in A for each individual condition, but no rows satisfy both conditions at once. 如果检查示例中的行,您会发现A中的每个单独条件都有一行,但是没有行同时满足这两个条件。 That is why you get nothing back: the row that satisfies a.id = b.pid does not satisfy a.id = c.pid , and vice versa. 这就是为什么您什么也得不到的原因:满足a.id = b.pid的行不满足a.id = c.pid ,反之亦然。

You could use an outer join to produce two results: 您可以使用外部联接来产生两个结果:

select *
  from A 
  left outer join B ON a.id = b.pid
  left outer join C ON a.id = c.pid;

a.id   a.name  b.pid   b.name    c.id   c.pid  c.name
  1  | Ruby  |   1   | Table B | NULL | NULL |  NULL
  2  | Java  | NULL  |  NULL   |  1   |   2  | Table C

Of course you return nothing! 当然,您什么也不会退! Table A and B inner join returns 1st record of Table A (table A ID = 1) then you join table C, there is no matching rows to join table C, and vice versa. 表A和B的内部联接返回表A的第一条记录(表A ID = 1),然后联接表C,没有匹配的行联接表C,反之亦然。

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

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