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,

以下是课程表,

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

and the class_info table,

class_info表,

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

Cross JOIN query will be,

Cross JOIN查询将是,

SELECT * FROM
class CROSS JOIN class_info;

The resultset table will look like,

结果集表如下所示:

ID NAME ID Address
1 abhi 1 DELHI
2 adam 1 DELHI
4 alex 1 DELHI
1 abhi 2 MUMBAI
2 adam 2 MUMBAI
4 alex 2 MUMBAI
1 abhi 3 CHENNAI
2 adam 3 CHENNAI
4 alex 3 CHENNAI
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,

考虑一个表,

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

and the class_info table,

class_info表,

ID Address
1 DELHI
2 MUMBAI
3 CHENNAI
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,

结果集表如下所示:

ID NAME ID Address
1 abhi 1 DELHI
2 adam 2 MUMBAI
3 alex 3 CHENNAI
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,

这是课程

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

and the class_info table,

class_info表,

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

Natural join query will be,

自然联接查询将是,

SELECT * from class NATURAL JOIN class_info;

The resultset table will look like,

结果集表如下所示:

ID NAME Address
1 abhi DELHI
2 adam MUMBAI
3 alex CHENNAI
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,

这是课程

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

and the class_info table,

class_info表,

ID Address
1 DELHI
2 MUMBAI
3 CHENNAI
7 NOIDA
8 PANIPAT
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,

结果集表如下所示:

ID NAME ID Address
1 abhi 1 DELHI
2 adam 2 MUMBAI
3 alex 3 CHENNAI
4 anu null null
5 ashish null null
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,

再一次上课

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

and the class_info table,

class_info表,

ID Address
1 DELHI
2 MUMBAI
3 CHENNAI
7 NOIDA
8 PANIPAT
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,

结果表如下所示:

ID NAME ID Address
1 abhi 1 DELHI
2 adam 2 MUMBAI
3 alex 3 CHENNAI
null null 7 NOIDA
null null 8 PANIPAT
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,

表,

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

and the class_info table,

class_info表,

ID Address
1 DELHI
2 MUMBAI
3 CHENNAI
7 NOIDA
8 PANIPAT
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,

结果集表如下所示:

ID NAME ID Address
1 abhi 1 DELHI
2 adam 2 MUMBAI
3 alex 3 CHENNAI
4 anu null null
5 ashish null null
null null 7 NOIDA
null null 8 PANIPAT
ID 名称 ID 地址
1个 阿比 1个 德里
2 亚当 2 孟买
3 亚历克斯 3 钦奈
4 阿努 空值 空值
5 空值 空值
空值 空值 7 野田
空值 空值 8 巴拿马型

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

sql横着连接起来sql

sql横着连接起来sql_SQL联接相关推荐

  1. sql横着连接起来sql_SQL联接的简要介绍(到目前为止)

    sql横着连接起来sql SQL Join是什么意思? (What does a SQL Join mean?) A SQL join describes the process of merging ...

  2. sql隐式连接和显示链接_SQL Server中的嵌套循环联接–批处理排序和隐式排序

    sql隐式连接和显示链接 In SQL Server, it`s not always required to fully understand the internal structure, esp ...

  3. SQL中内连接、外连接、交叉连接

    SQL中内连接.外连接.交叉连接 SQL连接可以分为内连接.外连接.交叉连接. 数据库数据:            book表                                      ...

  4. SQL Server 2005 连接本地端口1433开启远程连接/登陆18456错误的解决方法

    SQL Server 2005 连接本地端口1433开启远程连接/登陆18456错误的解决方法 出处:http://jimmyli.blog.51cto.com/我站在巨人肩膀上Jimmy Li 作者 ...

  5. SQL 左外连接,右外连接,全连接,内连

    SQL 左外连接,右外连接,全连接,内连接       连接条件可在FROM或WHERE子句中指定,建议在FROM子句中指定连接条件.WHERE和HAVING子句也可以包含搜索条件,以进一步筛选连接条 ...

  6. SQL三种连接:内连接、外连接、交叉连接

    SQL三种连接:内连接.外连接.交叉连接 一.交叉连接(CROSS JOIN) 二.内连接(INNER JOIN) 三.外连接(LEFT JOIN.RIGHT JOIN.FULL JOIN) 1.左外 ...

  7. SQL 左外连接,右外连接,全连接,内连接带图详细介绍

    SELECT id, name,description,img_url,sort,is_display ​ from bbs_brand ORDER BY id DESC ​ limit startR ...

  8. SQL Server 表连接

    目录 SQL Server 内连接 SQL Server 左连接 ON与WHERE子句 SQL Server 右连接 SQL Server 交叉连接 SQL Server自连接 SQL Server全 ...

  9. 左右链接php,sql的左连接和右连接有什么区别

    sql的左连接和右连接区别:1.左连接只要左边表中有记录,数据就能检索出来,而右连接是只要右边表中有记录:2.左连接是已左边表中的数据为基准,而右联接是左向外联接的反向联接. 本文操作环境:Windo ...

最新文章

  1. java注解编程_java 注解 基本原理 编程实现
  2. ehcache 清除缓存_如何设计一个本地缓存
  3. b站在线解析_这款游戏被全B站所唾弃,每个月却依然有5000万玩家坚持在线?!...
  4. 哈工大人工智能作业一_想从事人工智能行业,考研选什么专业对口?
  5. PAT乙级(1020 月饼)
  6. delta对冲策略_股票基础知识—Delta中性理论
  7. 那些月薪过万的程序员都是从什么时间开始的?
  8. 百度宣布成立量子计算研究所,段润尧加盟任所长
  9. POJ NOI0105-32 求分数序列和
  10. HDU-4324 Triangle LOVE 拓扑排序
  11. [2019杭电多校第五场][hdu6628]permutation 1
  12. word批量转换为pdf python脚本
  13. 2018年1月份最热门的JavaScript开源项目
  14. 移动APP卡顿问题解决实践
  15. linux 32位浏览器下载,Chrome 浏览器32位、64位下载地址大全
  16. VTK可交互三维坐标轴
  17. idea本地项目push到远程仓库报错解决方法
  18. continue用法
  19. 《回忆之前,忘记之后---写给我记忆中的汪峰》
  20. 【计算机组成原理】第2章 数据的表示和运算

热门文章

  1. 【 反向传播算法 Back-Propagation 数学推导以及源码详解 深度学习 Pytorch笔记 B站刘二大人(3/10)】
  2. MySQL字符集不一致的解决办法总结
  3. iCloud邮件客户端配置
  4. 破解美团外卖的 _token算法
  5. 鸡兔同笼python程序怎么写_鸡肉的做法大全_怎么做好吃_家常做法_下厨房
  6. k20pro刷鸿蒙,红米K20系列支持NFC功能吗 RedmiK20Pro手机能刷公交卡吗
  7. 如何举报可疑的Android应用
  8. Python3 pip安装-Star.hou
  9. 2022年商标注册需要多长时间?
  10. mysql 空间索引_Mysql空间索引