sql横着连接起来sql_SQL联接

sql横着连接起来sql

SQL Join is used to fetch data from two or more tables, which is joined to appear as single set of data. It is used for combining column from two or more tables by using values common to both tables.

SQL Join用于从两个或多个表中获取数据,这些表被连接起来以显示为单个数据集。 它用于通过使用两个表或两个表的公用值来合并两个或多个表的列。

JOIN Keyword is used in SQL queries for joining two or more tables. Minimum required condition for joining table, is (n-1) where n, is number of tables. A table can also join to itself, which is known as, Self Join.

JOIN关键字在SQL查询中用于JOIN两个或多个表。 联接表的最低要求条件是(n-1) ,其中n是表的数量。 表也​​可以联接到自身,这称为Self Join

JOIN的类型 (Types of JOIN)

Following are the types of JOIN that we can use in SQL:

以下是我们可以在SQL中使用的JOIN类型:

  • Inner

  • Outer

  • Left

    剩下

  • Right

交叉联接或笛卡尔积 (Cross JOIN or Cartesian Product)

This type of JOIN returns the cartesian product of rows from the tables in Join. It will return a table which consists of records which combines each row from the first table with each row of the second table.

这种JOIN类型返回Join中表中行的笛卡尔积。 它将返回一个包含记录的表,该记录将第一张表中的每一行与第二张表中的每一行组合在一起。

Cross JOIN Syntax is,

Cross JOIN语法是,

SELECT column-name-list
FROM 
table-name1 CROSS JOIN table-name2;
交叉联接示例 (Example of Cross JOIN)

Following is the class table,

以下是课程表,

IDNAME
1abhi
2adam
4alex
ID 名称
1个 阿比
2 亚当
4 亚历克斯

and the class_info table,

class_info表,

IDAddress
1DELHI
2MUMBAI
3CHENNAI
ID 地址
1个 德里
2 孟买
3 钦奈

Cross JOIN query will be,

Cross JOIN查询将是,

SELECT * FROM 
class CROSS JOIN class_info;

The resultset table will look like,

结果集表如下所示:

IDNAMEIDAddress
1abhi1DELHI
2adam1DELHI
4alex1DELHI
1abhi2MUMBAI
2adam2MUMBAI
4alex2MUMBAI
1abhi3CHENNAI
2adam3CHENNAI
4alex3CHENNAI
ID 名称 ID 地址
1个 阿比 1个 德里
2 亚当 1个 德里
4 亚历克斯 1个 德里
1个 阿比 2 孟买
2 亚当 2 孟买
4 亚历克斯 2 孟买
1个 阿比 3 钦奈
2 亚当 3 钦奈
4 亚历克斯 3 钦奈

As you can see, this join returns the cross product of all the records present in both the tables.

如您所见,此联接返回两个表中所有记录的叉积。

内部联接或EQUI联接 (INNER Join or EQUI Join)

This is a simple JOIN in which the result is based on matched data as per the equality condition specified in the SQL query.

这是一个简单的JOIN,其中结果根据SQL查询中指定的相等条件基于匹配的数据。

Inner Join Syntax is,

内部联接语法是,

SELECT column-name-list FROM 
table-name1 INNER JOIN table-name2 
WHERE table-name1.column-name = table-name2.column-name;
INNER JOIN的示例 (Example of INNER JOIN)

Consider a class table,

考虑一个表,

IDNAME
1abhi
2adam
3alex
4anu
ID 名称
1个 阿比
2 亚当
3 亚历克斯
4 阿努

and the class_info table,

class_info表,

IDAddress
1DELHI
2MUMBAI
3CHENNAI
ID 地址
1个 德里
2 孟买
3 钦奈

Inner JOIN query will be,

内部 JOIN查询将是,

SELECT * from class INNER JOIN class_info where class.id = class_info.id;

The resultset table will look like,

结果集表如下所示:

IDNAMEIDAddress
1abhi1DELHI
2adam2MUMBAI
3alex3CHENNAI
ID 名称 ID 地址
1个 阿比 1个 德里
2 亚当 2 孟买
3 亚历克斯 3 钦奈

自然加入 (Natural JOIN)

Natural Join is a type of Inner join which is based on column having same name and same datatype present in both the tables to be joined.

自然联接是内部联接的一种,它基于要连接的两个表中存在相同名称和相同数据类型的列。

The syntax for Natural Join is,

自然联接的语法是,

SELECT * FROM 
table-name1 NATURAL JOIN table-name2;
自然加入的例子 (Example of Natural JOIN)

Here is the class table,

这是课程

IDNAME
1abhi
2adam
3alex
4anu
ID 名称
1个 阿比
2 亚当
3 亚历克斯
4 阿努

and the class_info table,

class_info表,

IDAddress
1DELHI
2MUMBAI
3CHENNAI
ID 地址
1个 德里
2 孟买
3 钦奈

Natural join query will be,

自然联接查询将是,

SELECT * from class NATURAL JOIN class_info;

The resultset table will look like,

结果集表如下所示:

IDNAMEAddress
1abhiDELHI
2adamMUMBAI
3alexCHENNAI
ID 名称 地址
1个 阿比 德里
2 亚当 孟买
3 亚历克斯 钦奈

In the above example, both the tables being joined have ID column(same name and same datatype), hence the records for which value of ID matches in both the tables will be the result of Natural Join of these two tables.

在上面的示例中,两个要连接的表都具有ID列(相同的名称和相同的数据类型),因此两个表中ID值匹配的记录将是这两个表的自然连接的结果。

外连接 (OUTER JOIN)

Outer Join is based on both matched and unmatched data. Outer Joins subdivide further into,

外部联接基于匹配和不匹配的数据。 外连接细分为

  1. Left Outer Join

    左外连接

  2. Right Outer Join

    右外连接

  3. Full Outer Join

    完全外部加入

左外连接 (LEFT Outer Join)

The left outer join returns a resultset table with the matched data from the two tables and then the remaining rows of the left table and null from the right table's columns.

左外部联接返回一个结果集表,该表具有两个表中匹配的数据 ,然后返回表的其余行,而表的列为null。

Syntax for Left Outer Join is,

左外部联接的语法是,

SELECT column-name-list FROM 
table-name1 LEFT OUTER JOIN table-name2
ON table-name1.column-name = table-name2.column-name;

To specify a condition, we use the ON keyword with Outer Join.

为了指定条件,我们在外部联接中使用ON关键字。

Left outer Join Syntax for Oracle is,

Oracle的左外部Join语法为,

SELECT column-name-list FROM 
table-name1, table-name2 on table-name1.column-name = table-name2.column-name(+);
左外连接示例 (Example of Left Outer Join)

Here is the class table,

这是课程

IDNAME
1abhi
2adam
3alex
4anu
5ashish
ID 名称
1个 阿比
2 亚当
3 亚历克斯
4 阿努
5

and the class_info table,

class_info表,

IDAddress
1DELHI
2MUMBAI
3CHENNAI
7NOIDA
8PANIPAT
ID 地址
1个 德里
2 孟买
3 钦奈
7 野田
8 巴拿马型

Left Outer Join query will be,

左外连接查询将是,

SELECT * FROM class LEFT OUTER JOIN class_info ON (class.id = class_info.id);

The resultset table will look like,

结果集表如下所示:

IDNAMEIDAddress
1abhi1DELHI
2adam2MUMBAI
3alex3CHENNAI
4anunullnull
5ashishnullnull
ID 名称 ID 地址
1个 阿比 1个 德里
2 亚当 2 孟买
3 亚历克斯 3 钦奈
4 阿努 空值 空值
5 空值 空值

右外连接 (RIGHT Outer Join)

The right outer join returns a resultset table with the matched data from the two tables being joined, then the remaining rows of the right table and null for the remaining left table's columns.

右边的外部联接返回一个结果集表,该结果集表具有要联接的两个表中的匹配数据 ,然后是右边表的其余行,其余左边表的列为null。

Syntax for Right Outer Join is,

右外部连接的语法是,

SELECT column-name-list FROM 
table-name1 RIGHT OUTER JOIN table-name2 
ON table-name1.column-name = table-name2.column-name;

Right outer Join Syntax for Oracle is,

Oracle的右外部Join语法是,

SELECT column-name-list FROM 
table-name1, table-name2 
ON table-name1.column-name(+) = table-name2.column-name;
右外连接示例 (Example of Right Outer Join)

Once again the class table,

再一次上课

IDNAME
1abhi
2adam
3alex
4anu
5ashish
ID 名称
1个 阿比
2 亚当
3 亚历克斯
4 阿努
5

and the class_info table,

class_info表,

IDAddress
1DELHI
2MUMBAI
3CHENNAI
7NOIDA
8PANIPAT
ID 地址
1个 德里
2 孟买
3 钦奈
7 野田
8 巴拿马型

Right Outer Join query will be,

右外部联接查询将是,

SELECT * FROM class RIGHT OUTER JOIN class_info ON (class.id = class_info.id);

The resultant table will look like,

结果表如下所示:

IDNAMEIDAddress
1abhi1DELHI
2adam2MUMBAI
3alex3CHENNAI
nullnull7NOIDA
nullnull8PANIPAT
ID 名称 ID 地址
1个 阿比 1个 德里
2 亚当 2 孟买
3 亚历克斯 3 钦奈
空值 空值 7 野田
空值 空值 8 巴拿马型

完全外部加入 (Full Outer Join)

The full outer join returns a resultset table with the matched data of two table then remaining rows of both left table and then the right table.

完全外部联接将返回一个结果集表,其中包含两个表的匹配数据 ,然后是两个表的剩余行,然后是表。

Syntax of Full Outer Join is,

完全外部联接的语法是,

SELECT column-name-list FROM 
table-name1 FULL OUTER JOIN table-name2
ON table-name1.column-name = table-name2.column-name;
完全外部联接的示例是, (Example of Full outer join is,)

The class table,

表,

IDNAME
1abhi
2adam
3alex
4anu
5ashish
ID 名称
1个 阿比
2 亚当
3 亚历克斯
4 阿努
5

and the class_info table,

class_info表,

IDAddress
1DELHI
2MUMBAI
3CHENNAI
7NOIDA
8PANIPAT
ID 地址
1个 德里
2 孟买
3 钦奈
7 野田
8 巴拿马型

Full Outer Join query will be like,

完全外部联接查询将是这样,

SELECT * FROM class FULL OUTER JOIN class_info ON (class.id = class_info.id);

The resultset table will look like,

结果集表如下所示:

IDNAMEIDAddress
1abhi1DELHI
2adam2MUMBAI
3alex3CHENNAI
4anunullnull
5ashishnullnull
nullnull7NOIDA
nullnull8PANIPAT
ID 名称 ID 地址
1个 阿比 1个 德里
2 亚当 2 孟买
3 亚历克斯 3 钦奈
4 阿努 空值 空值
5 空值 空值
空值 空值 7 野田
空值 空值 8 巴拿马型

翻译自: https://www.studytonight.com/dbms/joining-in-sql.php

sql横着连接起来sql

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值