在SQL中联接多个表 [英] Joining multiple tables in SQL

查看:123
本文介绍了在SQL中联接多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我介绍有关加入的信息吗?

Can sombody Explains me about joins?

内部联接根据条件选择公共数据.

Inner join selects common data based on where condition.

左外部联接从左选择所有数据,而与公共无关,但从右表获取公共数据,反之亦然.

Left outer join selects all data from left irrespective of common but takes common data from right table and vice versa for Right outer.

我知道基本知识,但是对于5、8、10个表进行联接时,问题仍然存在.

I know the basics but question stays when it comes to join for than 5, 8, 10 tables.

假设我要加入10个表.如果我对前5个表进行了内部联接,现在尝试对第6个表进行左联接,那么查询将如何工作?

Suppose I have 10 tables to join. If I have inner join with the first 5 tables and now try to apply a left join with the 6th table, now how the query will work?

我的意思是说现在前5个表的结果集将被视为左表,而第6个表的结果集将被视为右表?还是只将第五张表视为左表,将第六张表视为右表?请为此提供帮助.

I mean to say now the result set of first 5 tables will be taken as left table and the 6th one will be considerded as Right table? Or only Fifth table will be considered as left and 6th as right? Please help me regarding this.

推荐答案

在联接多个表时,每个联接的输出在逻辑上形成一个虚拟表,该虚拟表将进入下一个联接.

When joining multiple tables the output of each join logically forms a virtual table that goes into the next join.

因此,在您所提问的示例中,连接前5个表的综合结果将被视为左侧表.

So in the example in your question the composite result of joining the first 5 tables would be treated as the left hand table.

有关更多信息,请参见 Itzik Ben-Gan的逻辑查询处理海报对这个.

See Itzik Ben-Gan's Logical Query Processing Poster for more about this.

可以通过放置ON子句来控制联接中涉及的虚拟表.例如

The virtual tables involved in the joins can be controlled by positioning the ON clause. For example

SELECT *
FROM   T1
       INNER JOIN T2
         ON T2.C = T1.C
       INNER JOIN T3
                  LEFT JOIN T4
                    ON T4.C = T3.C
         ON T3.C = T2.C 

等同于(T1 Inner Join T2) Inner Join (T3 Left Join T4)

这篇关于在SQL中联接多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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