inner join,outer join,left join,right join的区别

外联接

外联接可以是左向外联接、右向外联接或完整外部联接。
在 FROM 子句中指定外联接时,可以由下列几组关键字中的一组指定:

LEFT JOIN 或 LEFT OUTER JOIN。
左向外联接的结果集包括 LEFT OUTER 子句中指定的左表的所有行,而不仅仅是联接列所匹配的行。如果左表的某行在右表中没有匹配行,则在相关联的结果集行中右表的所有选择列表列均为空值。

RIGHT JOIN 或 RIGHT OUTER JOIN。
右向外联接是左向外联接的反向联接。将返回右表的所有行。如果右表的某行在左表中没有匹配行,则将为左表返回空值。

FULL JOIN 或 FULL OUTER JOIN。
完整外部联接返回左表和右表中的所有行。当某行在另一个表中没有匹配行时,则另一个表的选择列表列包含空值。如果表之间有匹配行,则整个结果集行包含基表的数据值。

例如:

表a

id              name

1               a
2               b
3               c

表b

id              store

1              15
2              10
4              67

inner join :

select * from a inner join b on a.id=b.id

结果:(只显示id匹配的选项)

id            name id store

1              a                 1             15
2              b                 2             10

内连接还有以下写法:

select * from a inner join b where a.id=b.id
select * from a,b where a.id=b.id(连接查询的另一种写法)
select * from a as e,b as r where e.id=r.id(使用as定义别名,当表名很长时有用)
select * from a e,b r where e.id=r.id(定义别名时可以省掉as)

外连接分左外连接和右外连接

左外连接:

select * from a left outer join b on a.id=b.id

或:

select * from a left join b on a.id=b.id

结果:(除了显示匹配记录,还显示a表中所有的记录)

id            name            id            stroe

1              a                 1             15
2              b                 2             10
3              c                /N             /N

右外连接:

select * from a right outer join b on a.id=b.id

或:
select * from a right join b on a.id=b.id

结果:(除了显示匹配的记录,还显示右表中所有的记录)

id            name            id            store

1              a                1              15
2              b                2              10
/N            /N               4              67

PS:本篇是翻译而来,原文如下:

Join types

By default, a join is assumed to be an inner join. You can also request other types of joins by clicking Join Type on the Joins page of SQL Assist. The following types of joins are available:

  • Inner join
  • Left outer join
  • Right outer join
  • Full outer join

7 An inner join is join method in which 7 a column that is not common to all of the tables being joined is dropped from 7 the resultant table. If your database supports the OUTER JOIN keywords, you 7 can extend the inner join to add rows from one table that have no matching 7 rows in the other table.

For example, you want to join two tables to get the last name of the manager for each department. The first table is a Department table that lists the employee number of each department manager. The second table is an Employee table that lists the employee number and last name of each employee. However, some departments do not have a manager; in these cases, the employee number of the department manager is null. To include all departments regardless of whether they have a manager, and the last name of the manager, if one exists, you create a left outer join. The left outer join includes rows in the first table that match the second table or are null. The resulting SQL statement is as follows:

SELECT DEPTNO, DEPTNAME, EMPNO, LASTNAME
        FROM DEPARTMENT LEFT OUTER JOIN EMPLOYEE
           ON MGRNO = EMPNO 

A right outer join is the same as a left outer join, except that it includes rows in the second table that match the first table or are null. A full outer join includes matching rows and null rows from both tables.

For example, you have two tables, Table 1 and Table 2, with the following data:

Table 1. Table 1
Column A Column B
1 A
2 B
3 C
Table 2. Table 2
Column C Column D
2 X
4 2

You specify a join condition of Column A = Column C. The result tables for the different types of joins are as follows:

Inner join
Table 3. Inner join result table
Column A Column B Column C Column D
2 B 2 X
Left outer join
Table 4. Left outer join result table
Column A Column B Column C Column D
1 A null null
2 B 2 X
3 C null null
Right outer join
Table 5. Right outer join result table
Column A Column B Column C Column D
2 B 2 X
null null 4 2
Full outer join
Table 6. Full outer join result table
Column A Column B Column C Column D
1 A null null
2 B 2 X
3 C null null
null null 4 2

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值