简体   繁体   English

从同一查询中不在左外连接中的表中获取字段

[英]Getting a field from a table that is not in the left outer join in the same query

I have 4 tables, 3 of which are joined, but I need to get a field from the forth table (Table_D). 我有4个表,其中3个已连接,但我需要从第四个表中获取一个字段(Table_D)。 For the sake of simplicity lets say they are Tables A, B, C, D. 为简单起见,我们假设它们是表A,B,C,D。

Select Distinct A.Field_1, B.Field_2, C.Field_3
From Table_A
Left outer join B on A.Field_z= B.Field_z
Left Outer Join C on A.Field_z= C.Field_z
where A.Field_z in (1111);

This seems to work but I need a field in Table_D that is only connected to Table_A through Table_C. 这似乎有效但我需要Table_D中的一个字段,它只通过Table_C连接到Table_A。

How can I add it to the join? 如何将其添加到联接中? or can I? 或者我可以吗?

Thanks! 谢谢!

WB WB

Of course you can add it. 当然你可以添加它。 You don't specify the logic, but something like this: 你没有指定逻辑,但是这样:

Select Distinct A.Field_1, B.Field_2, C.Field_3, D.??
From Table_A a Left outer join
     B
     on A.Field_z= B.Field_z Left Outer Join
     C
     on A.Field_z= C.Field_z Left Outer Join
     D
     on . . . 
where A.Field_z in (1111);

Just fill in the conditions that you want. 只需填写您想要的条件。 You want a left join , because the condition between c and d would otherwise turn the outer join to c into an inner join. 您想要一个left join ,因为cd之间的条件会将外连接转换为c到内连接。

如果我理解正确,我相信这会奏效:

Left Outer Join D on C.Field_z= D.Field_z

You can try this way. 你可以试试这种方式。 It will help i guess 这将有助于我猜

Select Distinct A.Field_1, B.Field_2, C.Field_3, c.Field_4
From Table_A
Left outer join B on A.Field_z= B.Field_z
Left Outer Join  ( select C.field_3, d.Field_4, c.Field_z from c inner join d on c.field_z = d.field_z ) c on A.Field_z= C.Field_z
where A.Field_z in (1111);

The on clause has essentially all the features of the where clause. on子句基本上具有where子句的所有功能。 on clauses for any part of the join can refer to any attribute of any entity in the from clause, I suspect your confusion comes from thinking that you can only join subsequent entities to the first entity in the from clause. 对于连接的任何部分的on子句可以引用from子句中任何实体的任何属性,我怀疑你的混淆来自于认为你只能将后续实体加入from子句中的第一个实体。 If Table_D is related to Table_A through Table_C , you might see a from / join / on construct like: 如果Table_DTable_ATable_C相关,您可能会看到from / join / on构造,如:

select a.thing, b.things, c.thang, d.stuff
from Table_A a
 left join Table_B b
  on a.id = b.a_id
 left join Table_C c
  on a.id = c.a_id
 left join Table D d
  on d.id = c.d_id
where
  a.this = b.that

Note that the word "outer" is implied in a left join, and is not necessary for syntactic completion. 请注意,单词“outer”隐含在左连接中,并不是语法完成所必需的。

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

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