sql中join类型

This article will provide an overview of the SQL Join and cover all of the SQL join types including inner, self, cross and outer. For inner joins we’ll be discussing Equi and Theta joins.

本文将概述SQL连接,并涵盖所有SQL连接类型,包括内部,自身,交叉和外部。 对于内部联接,我们将讨论Equi和Theta联接。

The ability to combine results from related rows from multiple tables is an important part of relational database system design. In SQL Server, this is accomplished with the SQL join clause. It’s the nature of traditional relational database systems where some table contains information related to other tables with a common key value. Using a SQL join, you can easily perform queries on related data-sets from multiple tables with these shared keys.

合并来自多个表的相关行的结果的能力是关系数据库系统设计的重要组成部分。 在SQL Server中,这是通过SQL join子句完成的。 这是传统关系数据库系统的本质,其中某些表包含具有其他键值的其他表的相关信息。 使用SQL连接,您可以使用这些共享键轻松地从多个表中对相关数据集执行查询。

The aim of this article is to provide you with the basic knowledge and examples that you will need to use the SQL join effectively in any database environment.

本文的目的是为您提供在任何数据库环境中有效使用SQL连接所需的基本知识和示例。

什么是SQL连接? (What is a SQL join?)

A SQL Join is a special form of generating a meaningful data by combining multiple tables relate to each other using a “Key”. Typically, relational tables must be designed with a unique column and this column is used to create relationships with one or more other tables. When you need a result-set that includes related rows from multiple tables, you’ll need to use SQL join on this column

SQL联接是一种特殊形式,可以通过使用“键”组合彼此相关的多个表来生成有意义的数据。 通常,关系表必须设计为具有唯一列,并且该列用于创建与一个或多个其他表的关系。 如果需要一个包含多个表中相关行的结果集,则需要在此列上使用SQL连接

The various SQL join types are as follows

各种SQL连接类型如下

    1. Equi join 平等加入
    2. Non-equi join (Theta join) 非等额联接(Theta联接)
    1. SQL left join or left outer join SQL左联接或左外部联接
    2. SQL right join or right outer join SQL右连接或右外连接
    3. SQL full join or full outer join SQL完全连接或完全外部连接
  1. SQL cross join SQL交叉连接
  2. SQL self join SQL自连接

Note: The keyword outer is optional. It means you can specify the keyword “outer” or not makes no difference to the query execution.

注意:关键字outer是可选的。 这意味着您可以指定关键字“ outer”,或者对查询执行没有影响。

For example,

例如,

SQL连接类型 (SQL join types)

SQL内部联接 (SQL inner join)

The simplest and most common form of a join is the SQL inner join the default of the SQL join types used in most database management systems. It’s the default SQL join you get when you use the join keyword by itself.

连接的最简单和最常见的形式是SQL内部连接,这是大多数数据库管理系统中使用SQL连接类型的默认值。 它是单独使用join关键字时获得的默认SQL连接。

The result of the SQL inner join includes rows from both the tables where the join conditions are met.

SQL内部联接的结果包括两个表中满足联接条件的行。

Syntax:

句法:

SELECT ColumnList from LeftTable L
INNER join  RightTable R
ON L.Column=R.Column

Note: It is very easy to visualize a join query as a Venn diagram, where each of the tables is represented by intersecting shapes. The intersection of the shapes, where the tables overlap, are the rows where a condition is met. Unique columns (ID) are often used for this purpose, where the condition to be met is matching the ids of rows.

注意:将联接查询可视化为维恩图非常容易,其中每个表都由相交的形状表示。 表格相交的形状的交点是满足条件的行。 唯一列(ID)通常用于此目的,其中要满足的条件与行的ID匹配。

平等加入: (Equi join:)

An equi join is the most common form of SQL inner join used in practice. If the join contains an equality operator e.g. =, then it’s an equi-join.

等联接是实践中使用的最常见SQL内部联接形式。 如果联接包含等号运算符,例如=, 则为等联接。

The following example returns all matching state names and stateProvinceIDs.

下面的示例返回所有匹配的状态名称和stateProvinceID。

SELECT DISTINCT A.StateProvinceID,S.Name
FROM Person.Address A
inner join Person.StateProvince S
On A.StateProvinceID=S.StateProvinceID

Theta连接(非设备连接): (Theta join (Non-equi join):)

In general, this a Theta join used to specify operators or conditions (the ON clause in SQL). In practice, this is a rarely used SQL join types. In most cases, the join will use a non-equality condition e.g. >

通常,此Theta联接用于指定运算符或条件(SQL中的ON子句)。 实际上,这是一种很少使用SQL连接类型。 在大多数情况下,联接将使用非相等条件,例如>

SELECT p1.FirstName, p2. FirstName
FROM PErson.Person p1
INNER join PErson.Person p2
ON len(p1.FirstName) > len(p2.FirstName);

SQL自连接 (SQL self join)

A SQL Self join is a mechanism of joining a table to itself. You would use a self join when you wanted to create a result set joining records in the table with some other records from the same table.

SQL Self连接是将表连接到自身的机制。 当您想要在表中创建记录集和同一表中的其他记录时,可以使用自连接。

For a SQL self join example, consider an Employee table where managers are listed because they are also employees, and we would like to take a look at a result set that returns all of the employees and indicating who their managers are

对于一个SQL自连接示例,考虑一个列出了经理的雇员表,因为他们也是雇员,我们想看看一个返回所有雇员并指出他们的经理是谁的结果集。

SELECT e.ename, e.empno, m.ename as manager, e.mgr
FROMemp e, emp m
WHERE e.mgr = m.empno

SQL交叉连接 (SQL cross join)

A CROSS join returns all rows for all possible combinations of two tables. It generates all the rows from the left table which is then combined with all the rows from the right table. This type of join is also known as a Cartesian product(A*B).

CROSS连接返回两个表的所有可能组合的所有行。 它从左侧表生成所有行,然后将其与右侧表中的所有行合并。 这种连接也称为笛卡尔乘积(A * B)。

For example, if the left table has 100 rows and the right table has 100 then the cross join result will yield 10,000 rows.

例如,如果左边的表有100行,右边的表有100行,那么交叉联接结果将产生10,000行。

SELECT e.BusinessEntityID, d.Name AS Department
FROM HumanResources.Employee AS e
CROSS join HumanResources.Department AS d

SQL外部联接 (SQL outer join)

On joining tables with a SQL inner join, the output returns only matching rows from both the tables. When using a SQL outer join, not only it will list the matching rows, it will also list the unmatched rows from the other tables.

使用SQL内部联接联接表时,输出仅返回两个表中匹配的行。 使用SQL外连接时,它不仅会列出匹配的行,还会列出其他表中不匹配的行。

A SQL left outer join will return all the records from the left table in the join clause, regardless of matching records in the right table. The left SQL outer join includes rows where the condition is met plus all the rows from the table on the left where the condition is not met. Fields from the right table with no match will be displayed as null values.

SQL左外部联接将返回join子句中左表中的所有记录,而不管右表中的记录是否匹配。 左SQL外连接包括满足条件的行以及左侧表中不满足条件的所有行。 右侧表中没有匹配项的字段将显示为空值。

Syntax:

句法:

SELECT ColumnList from LeftTable L
LEFT join  RightTable R
ON L.Column=R.Column
Where R.Column is NULL

The following example joins two tablesProduct and SalesOrderDetail on ProductID and preserves the unmatched rows from the left table. The Product table is matched with the SalesOrderDetail table on the ProductID columns in each table. All products, ordered and not ordered, appear in the result set.

以下示例在ProductID上连接两个表Product和SalesOrderDetail,并保留左侧表中不匹配的行。 产品表与每个表中产品ID列上的SalesOrderDetail表匹配。 所有产品(已订购和未订购)都出现在结果集中。

SELECT p.Name, so.SalesOrderID
FROM Production.Product  p
LEFT OUTER join Sales.SalesOrderDetail so
ON p.ProductID = so.ProductID
ORDER BY p.Name ;

A right outer join will return all the records in the right table in the join clause, regardless of matching records in the left table. Using the right SQL outer join includes all the rows from the table on the right. The right SQL outer join is considered a special case and many databases don’t support right joins. Generally, a SQL right join can be rewritten as a SQL left join by simply changing the order of the tables in the query. In this instance, fields from the left table with no match will display null values

右外部联接将返回join子句右表中的所有记录,而不管左表中的记录是否匹配。 使用正确SQL外连接包括右边表中的所有行。 正确SQL外连接被认为是一种特殊情况,许多数据库不支持正确的连接。 通常,只需更改查询中表的顺序,就可以将SQL右连接重写为SQL左连接。 在这种情况下,左侧表格中没有匹配项的字段将显示空值

Syntax:

句法:

SELECT ColumnList from LeftTable L
RIGHT join  RightTable R
ON L.Column=R.Column
Where L.Column is NULL

The following example joins two tables on TerritoryID(SalesTerritory) and preserves the unmatched rows from the right table(SalesPerson). The SalesTerritory table is matched with the SalesPerson table on the TerritoryID column in each table. All salespersons appear in the result set, whether or not they are assigned a territory.

下面的示例在TerritoryID(SalesTerritory)上联接两个表,并保留右表(SalesPerson)中不匹配的行。 SalesTerritory表与每个表的TerritoryID列上的SalesPerson表匹配。 无论是否分配了区域,所有销售人员都会出现在结果集中。

SELECT s.Name AS Territory, p.BusinessEntityID
FROM Sales.SalesTerritory  s
RIGHT OUTER join Sales.SalesPerson p
ON s.TerritoryID = p.TerritoryID ;

A SQL outer join, as you might expect by now, will return all the rows in both tables. When rows don’t have a match in one of the tables, the field will display a null value. A full SQL outer join combines the effects of the SQL left joins and SQL right joins. Many databases do not support the implementation of full SQL outer joins

正如您现在所期望的那样, SQL外连接将返回两个表中的所有行。 当其中一张表中的行不匹配时,该字段将显示一个空值。 完整SQL外连接结合了SQL左连接和SQL右连接的效果。 许多数据库不支持完整SQL外部联接的实现

Syntax:

句法:

SELECT ColumnList from LeftTable L
FULL OUTER join  RightTable R
ON L.Column=R.Column

The following example returns the name of the product name any corresponding sales orders in the SalesOrderDetail table from the AdventureWorks2014 database. It also returns any sales orders that have no product listed in the Product table, and any products with a sales order other than the one listed in the Product table.

以下示例从AdventureWorks2014数据库中的SalesOrderDetail表中返回任何对应的销售订单的产品名称的名称。 它还返回“产品”表中未列出任何产品的任何销售订单,以及“产品”表中未列出任何销售订单的产品。

SELECT p.Name, s.SalesOrderID
FROM Production.Product p
FULL OUTER join Sales.SalesOrderDetail s
ON p.ProductID = s.ProductID
ORDER BY p.Name ;

摘要 (Summary)

In this article, we’ve discussed most of the important aspects of SQL Joins and covered a variety of SQL join types. We’ve also demonstrated a few quick examples and samples of how we can pull data from related tables from the Adventureworks2016 database and how those tables actually get that relationship through the use of those keys using SQL joins.

在本文中,我们讨论了SQL连接的大多数重要方面,并介绍了各种SQL连接类型。 我们还演示了一些快速的示例和示例,这些示例和示例说明了如何从Adventureworks2016数据库的相关表中提取数据,以及这些表如何通过使用SQL连接使用这些键来实际获得这种关系。

That’s all for now. I hope you enjoyed this article on SQL Join types. Feel free ask any questions in the comments below.

目前为止就这样了。 希望您喜欢这篇有关SQL Join类型的文章。 请随时在下面的评论中提问。

翻译自: https://www.sqlshack.com/sql-join-overview-and-tutorial/

sql中join类型

sql中join类型_SQL Join类型概述和教程相关推荐

  1. 【转载】SQL中使用update inner join和delete inner join

    原文地址:SQL中使用update inner join和delete inner join Update XXX set XXX where 这种写法大家肯定都知道,才发现update和delete ...

  2. SQL中的left outer join,inner join,right outer join用法 (左右内连接)

    SQL语句中的left outer join,inner join,right outer join用法 left outer join=left join ,   right outer join= ...

  3. sql中变量用法_SQL变量:基础和用法

    sql中变量用法 In this article, we will learn the notions and usage details of the SQL variable. In SQL Se ...

  4. SQL中的left outer join,inner join,right outer join用法

    使用关系代数合并数据 1 关系代数 合并数据集合的理论基础是关系代数,它是由E.F.Codd于1970年提出的. 在关系代数的形式化语言中: ?          用表.或者数据集合表示关系或者实体. ...

  5. SQL中的left outer join,inner join,right outer join用法详解1

    LEFT JOIN 关键字会从左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中没有匹配的行. LEFT JOIN 关键字语法 SELECT column_ ...

  6. SQL中的left outer join,inner join,right outer join用法详解

    SQL提供了多种类型的连接方式,它们之间的区别在于:从相互交叠的不同数据集合中选择用于连接的行时所采用的方法不同. 连接类型                                       ...

  7. sql 表变量 临时表_SQL表变量概述

    sql 表变量 临时表 This article explores the SQL Table variables and their usage using different examples. ...

  8. sql中排序序号_SQL 和 SPL 的有序运算对比

    [摘要] 有序运算是指按照一定的次序对有序集合的成员进行计算.SQL 和 SPL 是大家比较熟悉的程序语言,本文将探讨对于有序运算问题,这两种语言的解决方案和基本原理.如何简便快捷的处理有序运算,这里 ...

  9. sql 自定义函数 示例_SQL滞后函数概述和示例

    sql 自定义函数 示例 In the article SQL Server Lead function overview and examples, we explored Lead functio ...

最新文章

  1. NBT封面:纳米孔基因组测序快速临床诊断细菌性下呼吸道感染
  2. python 数组在最前面插入数据_python – 如何将数组插入数据库?
  3. 视频质量评估的新方式:VMAF百分位数
  4. xcode-select: error: tool 'xcodebuild' requires Xcode错误解决方法
  5. Windows中安装MongoDB以及studio3t
  6. Java的Stack类
  7. 一周内被程序员疯转 2.4 W次,最终被大厂封杀!
  8. maven为什么删除了一致弹出这个模块的文件夹_史诗级!有手就会的Maven教程(四)...
  9. Volatile(理解)
  10. MySQL__数据处理之查询
  11. 【内推】AI独角兽-数美科技-NLP/CV/ASR等开放百余岗位,薪资诱人
  12. 电磁波中的波段划分:L波段、S波段、C波段、X波段、Ku波段、K波段、Ka波段 等等旧的无线电波段划分中L、S、C、X、Ku、Ka、W波段频率分为分别是多少? 这种划
  13. 异步时钟域的亚稳态问题和同步器(四)
  14. 艾司博讯:拼多多商家sku编码是什么意思
  15. BCH采用大爆发,支付理念深入人心
  16. 22岁天才少女加入华为,曾获“编程界的奥林匹克”世界冠军
  17. Openbravo安装与部署
  18. 自建 CA 中心并签发 CA 证书
  19. csdner: china_jeffery, C++默认构造函数; csdner: thief thief, 什么情况下C++编译器会生成默认的构造函数
  20. 动态规划问题 -- 求给定K个币种时N美元有几种组合方式 (例1,2,3美元存在,求10美元有几种组合方式)

热门文章

  1. 电脑常用音频剪辑软件_常用的音频编辑软件
  2. 2018年慈溪职高计算机实验班2019,包场高级中学(创新实验班)2019年中考录取分数线...
  3. python周期执行-用Python执行周期性动作
  4. 删除ubuntu旧内核
  5. 安卓开发笔记(十):升级ListView为RecylerView的使用
  6. ubuntu HackRF One相关环境搭建
  7. Jenkins 自动化集成之路 Linux 安装 maven
  8. mysql分表和分区的区别
  9. CodeFirst 表之间的关联
  10. 嵌入式实时操作系统的可裁剪性及其实现