【问题标题】:outer join -- restriction on From clause外连接——对 From 子句的限制
【发布时间】:2016-05-10 19:45:45
【问题描述】:

我是 SQL 新手。我遇到的问题是,当我使用 LEFT JOIN 时,我试图从逻辑上理解对 From 子句的限制,我已经学习了对 where 子句的限制,但我以前从未见过这个。我看了这篇文章,https://stackoverflow.com/questions/8311096/whats-the-difference-between-where-clause-and-on-clause-when-table-left-join#= 但我还是一头雾水。

所以我的代码是这样的。

Select e.last_name
      ,e.first_name
      ,e.marital_status_code
      ,ms.short_desc
from entity e
left join marital_status ms 
     on e.marital_status_code = ms.marital_status_code AND ms.marital_status_code = 'M'
where e.last_name = 'Hello'
order by 3
;

当我对 Where 子句进行限制时,我完全明白了

喜欢

where e.last_name = 'Hello'
AND marital_status_code = 'M'
 order by 3
    ;

当我像这样对 From 子句施加限制时,请帮助我发生了什么/逻辑是如何工作的

left join marital_status ms 
         on e.marital_status_code = ms.marital_status_code AND ms.marital_status_code = 'M'

【问题讨论】:

    标签: sql outer-join


    【解决方案1】:
    left join marital_status ms 
         on e.marital_status_code = ms.marital_status_code AND ms.marital_status_code = 'M'
    

    这是连接的一个条件。

    这是对何时匹配两行的限制。因此,当两个表具有相同的 marital_status_code 并且 marital_status_code 在 marital_table 中为 = 'M' 时,这将在另一个表中找到匹配项。

    IS THE SAME 与没有加入并说

    WHERE e.marital_status_code = 'M'
    

    如果 marital_status 表中只有一行 marital_status_code 为“M”

    或者如果 marital_status 表中没有这样的行,则它是相同的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多