sql中join类型_SQL Join类型概述和教程

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自连接

SQL join types

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 join types examples

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内部联接的结果包括两个表中满足联接条件的行。

SQL inner join

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

SQL equi join results example
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 theta join results example

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连接是将表连接到自身的机制。 当您想要在表中创建记录集和同一表中的其他记录时,可以使用自连接。

SQL self join

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
FROM
    emp e, emp m
WHERE e.mgr = m.empno

SQL self join example results

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行。

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

SQL cross join results example

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外连接时,它不仅会列出匹配的行,还会列出其他表中不匹配的行。

SQL outer join

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外连接包括满足条件的行以及左侧表中不满足条件的所有行。 右侧表中没有匹配项的字段将显示为空值。

SQL left outer join

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 ;

SQL left outer join results example

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左连接。 在这种情况下,左侧表格中没有匹配项的字段将显示空值

right outer join

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 ;

right outer join results example

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外部联接的实现

SQL outer join

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 ;

SQL outer join results example

摘要 (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类型

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值