简体   繁体   English

Join子句以相同条件连接3个表

[英]Join clause joining 3 tables in same criteria

I've saw a join just like this: 我看到了这样的联接:

Select <blablabla>
from 
  TableA TA
  Inner join TableB TB on Ta.Id = Tb.Id
  Inner join TableC TC on Tc.Id = Tb.Id and Ta.OtheriD = Tc.OtherColumn

But what's the point (end effect) of that second join clause? 但是第二个join子句有什么意义(最终效果)?
What the implications when an outer join clause is used? 使用外部连接子句的含义是什么?
And, more important, what is the best to rewrite it in a way that is easy to understand what it's trying to join? 而且,更重要的是,以一种易于理解的方式尝试重写的最佳方法是什么?
And, more important, what is the best way to rewrite it to get rid of the construction and mantain the correctness of the query. 而且,更重要的是,重写它的最佳方法是摆脱构造并保持查询的正确性。
I don't specify the RDBMS, because it's a more generic question, but for those curious (since people always ask): it's SQL Server 2005. 我没有指定RDBMS,因为它是一个更通用的问题,但是对于那些好奇的人(因为人们总是问):它是SQL Server 2005。

EDIT: It's just a made up example (since I would have to dig the original source - which I don't have access anymore). 编辑:这只是一个虚构的示例(因为我将不得不挖掘原始源-我已经无权访问了)。 I found the original join clause on a 10 join SELECT command. 我在10 join SELECT命令上找到了原始的join子句。

It simply means you have an extra restriction on the intersection between tablea and tablec. 这仅表示您对tablea和tablec之间的交集有额外的限制。

Because we know Ta.Id = Tb.Id , Tc.Id = Tb.Id is the same as Tc.Id = Ta.Id . 因为我们知道Ta.Id = Tb.IdTc.Id = Tb.IdTc.Id = Ta.Id相同。 Inner joins are associative. 内部联接是关联的。 So it makes more sense like this so each join is between 2 tables only 这样更有意义,因此每个联接仅在2个表之间

Select <blablabla>
from 
  TableB TB 
  Inner join
  TableA TA on Tb.Id = Ta.Id   --a and b intersection
  Inner join
  TableC TC on Ta.Id = Tc.Id and Ta.OtheriD = Tc.Column   --a and c intersection

Your Q : But what's the point (end effect) of that second join clause? 您的问:但是第二个join子句有什么意义(最终效果)?

Effectively filters rows...you could move the second half of the on statement into the where clause if you really want, only really effects readability. 有效地过滤行...如果您确实愿意,可以将on语句的后半部分移到where子句中,只会真正影响可读性。 gbn's answer looks good for this 3 table example,but to expand on it...sometimes a rewrite like this isn't possible. gbn的答案对于此3表示例看起来不错,但要对其进行扩展...有时无法进行这样的重写。 I have seen an occasion where 2 different systems (one oracle 8i and one SQL server 2000) had their databases joined together. 我见过一个场合,两个不同的系统(一个oracle 8i和一个SQL Server 2000)将它们的数据库连接在一起。 A 3 part key was identified as being required to make the records unique in both systems, but each component of the 3 part key was held in different tables...the final result had a few joins like that. 确定了在两个系统中使记录唯一所必需的3部分密钥,但是3部分密钥的每个组件都保存在不同的表中……最终结果具有这样的一些联接。

Functionally...I'm not sure if there's a difference really. 从功能上来说...我不确定是否真的有区别。 Unless I'm completely off, readability seems to be the biggest difference. 除非我完全不了解,否则可读性似乎是最大的不同。

Your Second Q: What the implications when an outer join clause is used? 您的第二个问题:使用外部连接子句会产生什么影响?

You'll potentially get a bunch of nulls (pending how you setup the outer join) while the inner join would have dropped them. 当内部联接将其丢弃时,您可能会得到一堆空值(等待设置外部联接的方式)。 Be careful though...inner joins is associative...as gbn put it: An OUTER JOIN is different and order does matter 尽管要小心……内部联接是关联的……正如gbn所说:外部联接是不同的,顺序很重要

用户可能想进一步过滤联接集中包含的行集...

The point of the second join is to further limit your result set based on the contents of TableC. 第二个联接的目的是根据TableC的内容进一步限制结果集。 The first join gives you ONLY records that exist in TA and TB. 第一次联接只为您提供TA和TB中存在的记录。 The second join gives you ONLY results from the first join that also exist in TC. 第二个联接仅提供TC中也存在的第一个联接的结果。

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

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