SQL Server - 结合 OUTER 和 INNER 连接 [英] SQL Server - Combining OUTER and INNER joins

查看:35
本文介绍了SQL Server - 结合 OUTER 和 INNER 连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 3 个表,我必须将表 A 与表 B 内部连接,但表 A 和表 C 之间有一个左外部连接.

I have 3 tables and I have to inner join Table A with Table B but a left outer between Table A and Table C.

我可以在同一个查询中组合外连接和内连接吗?我可以嵌套查询并获得所需的结果,但我无法在同一个查询中进行这两个连接.似乎在其他 SQL 语言中,加入的顺序很重要.在 SQL Server 中也是这样吗?

Can I combine outer and inner join in the same query? I can nest queries and attain the desired result but I am unable to do both the joins in the same query. It appears that in other SQL languages, the order of joining is important. Is this the case in SQL Server as well?

好的,这是场景.

考虑 3 张桌子.表A、表F、表D.

Consider 3 tables. Table A, Table F, Table D.

我需要记录集来包含 D 中的所有行,而不管它是否存在于 F 中(在它与 A 内部连接之后).因此,想到了外部联接.我需要的是:

I will need the recordset to contain all rows in D irrespective of whether it exists in F (after it's inner joined with A). So, a outer join comes to mind. What I would need is:

  1. 首先在 A 和 F 之间做一个内连接得到一个集合(这可能是一个空集合)
  2. 然后使用 D 对 (1) 中的记录集进行外连接

推荐答案

如果我理解正确,你想要这样的东西:

If I understand correctly you want something like this:

select
    *
from
    a 
    left outer join c
        inner join b on c.bID = b.ID
    on a.cID = c.ID

这篇关于SQL Server - 结合 OUTER 和 INNER 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆