按:SQL术语Join在中文对应的翻译是“连接”还是“联结”说法不一,下文将统一采用“连接”的译法。

开局一张图:

前奏/准备工作:

Emp表结构:

create table emp(
empid number(4,0),
empname nvarchar2(20),
deptno number(4,0)
)

Emp充值语句:

insert into emp(empid,empname,deptno) values('','Andy','');
insert into emp(empid,empname,deptno) values('','Bill','');
insert into emp(empid,empname,deptno) values('','Cindy','');
insert into emp(empid,empname,deptno) values('','Douglas','');
insert into emp(empid,empname,deptno) values('','张三','');
insert into emp(empid,empname,deptno) values('','李四','');
insert into emp(empid,empname,deptno) values('','王五','');

Dept表结构:

create table dept(
deptid number(4,0),
deptname nvarchar2(20)
)

Dept充值语句:

insert into dept(deptid,deptname) values('','研发');
insert into dept(deptid,deptname) values('','销售');
insert into dept(deptid,deptname) values('','市场');
insert into dept(deptid,deptname) values('','管理');
insert into dept(deptid,deptname) values('','公关');
insert into dept(deptid,deptname) values('','咨询');

正文:

内连接 :

SQL语句:

select emp.*,dept.* from emp inner join dept on emp.deptno=dept.deptid

查询结果:

SQL> select emp.*,dept.* from emp inner join dept on emp.deptno=dept.deptid;

     EMPID EMPNAME                                      DEPTNO     DEPTID DEPTNAME
---------- ---------------------------------------- ---------- ---------- ----------------------------------------
2 Bill 1 1 研发
4 Douglas 2 2 销售
3 Cindy 2 2 销售
5 张三 4 4 管理 已用时间: 00: 00: 00.01

左连接:

SQL语句:

select emp.*,dept.* from emp left join dept on emp.deptno=dept.deptid

查询结果:

SQL> select emp.*,dept.* from emp left join dept on emp.deptno=dept.deptid;

     EMPID EMPNAME                                      DEPTNO     DEPTID DEPTNAME
---------- ---------------------------------------- ---------- ---------- ----------------------------------------
2 Bill 1 1 研发
4 Douglas 2 2 销售
3 Cindy 2 2 销售
5 张三 4 4 管理
6 李四 6
7 王五 7 已选择6行。 已用时间: 00: 00: 00.00

右连接:

SQL:

select emp.*,dept.* from emp right join dept on emp.deptno=dept.deptid

查询结果:

SQL> select emp.*,dept.* from emp right join dept on emp.deptno=dept.deptid;

     EMPID EMPNAME                                      DEPTNO     DEPTID DEPTNAME
---------- ---------------------------------------- ---------- ---------- ----------------------------------------
2 Bill 1 1 研发
3 Cindy 2 2 销售
4 Douglas 2 2 销售
5 张三 4 4 管理
5 公关
8 咨询
3 市场 已选择7行。 已用时间: 00: 00: 00.01

左连接去除内连

SQL:

select emp.*,dept.* from emp left join dept on emp.deptno=dept.deptid where dept.deptid IS NULL

查询结果:

SQL> select emp.*,dept.* from emp left join dept on emp.deptno=dept.deptid where dept.deptid IS NULL;

     EMPID EMPNAME                                      DEPTNO     DEPTID DEPTNAME
---------- ---------------------------------------- ---------- ---------- ----------------------------------------
6 李四 6
7 王五 7 已用时间: 00: 00: 00.00

右连接去除内连

SQL:

select emp.*,dept.* from emp right join dept on emp.deptno=dept.deptid where emp.deptno IS NULL

查询结果:

SQL> select emp.*,dept.* from emp right join dept on emp.deptno=dept.deptid where emp.deptno IS NULL;

     EMPID EMPNAME                                      DEPTNO     DEPTID DEPTNAME
---------- ---------------------------------------- ---------- ---------- ----------------------------------------
5 公关
8 咨询
3 市场 已用时间: 00: 00: 00.01

全连接

SQL:

select emp.*,dept.* from emp full join dept on emp.deptno=dept.deptid 

查询结果:

SQL> select emp.*,dept.* from emp full join dept on emp.deptno=dept.deptid ;

     EMPID EMPNAME                                      DEPTNO     DEPTID DEPTNAME
---------- ---------------------------------------- ---------- ---------- ----------------------------------------
2 Bill 1 1 研发
4 Douglas 2 2 销售
3 Cindy 2 2 销售
3 市场
5 张三 4 4 管理
5 公关
8 咨询
6 李四 6
7 王五 7 已选择9行。 已用时间: 00: 00: 00.01

全连接去除内连接,这种查询适合比较两个结果集的差异,具体请见https://www.cnblogs.com/xiandedanteng/p/12239597.html

SQL:

select emp.*,dept.* from emp full join dept on emp.deptno=dept.deptid where emp.deptno IS NULL or dept.deptid IS NULL

查询结果:

SQL> select emp.*,dept.* from emp full join dept on emp.deptno=dept.deptid where emp.deptno IS NULL or dept.deptid IS NULL;

     EMPID EMPNAME                                      DEPTNO     DEPTID DEPTNAME
---------- ---------------------------------------- ---------- ---------- ----------------------------------------
3 市场
5 公关
8 咨询
6 李四 6
7 王五 7 已用时间: 00: 00: 00.00

全外连接去除内连接

SQL:

select emp.*,dept.* from emp full outer join dept on emp.deptno=dept.deptid where emp.deptno IS NULL or dept.deptid IS NULL

查询结果:

SQL> select emp.*,dept.* from emp full outer join dept on emp.deptno=dept.deptid where emp.deptno IS NULL or dept.deptid IS NULL;

     EMPID EMPNAME                                      DEPTNO     DEPTID DEPTNAME
---------- ---------------------------------------- ---------- ---------- ----------------------------------------
3 市场
5 公关
8 咨询
6 李四 6
7 王五 7 已用时间: 00: 00: 00.00

IN半连接:

SQL:

select emp.* from emp where emp.deptno in (select deptid from dept)

查询结果:

SQL> select emp.* from emp where emp.deptno in (select deptid from dept);

     EMPID EMPNAME                                      DEPTNO
---------- ---------------------------------------- ----------
2 Bill 1
4 Douglas 2
3 Cindy 2
5 张三 4 已用时间: 00: 00: 00.00

EXISTS半连接:

SQL:

select emp.* from emp where exists  (select NULL from dept where dept.deptid=emp.deptno)

查询结果:

SQL> select emp.* from emp where exists  (select NULL from dept where dept.deptid=emp.deptno);

     EMPID EMPNAME                                      DEPTNO
---------- ---------------------------------------- ----------
2 Bill 1
4 Douglas 2
3 Cindy 2
5 张三 4 已用时间: 00: 00: 00.01

IN反连接:

SQL:

select emp.* from emp where emp.deptno not in (select deptid from dept)

查询结果:

SQL> select emp.* from emp where emp.deptno not in (select deptid from dept);

     EMPID EMPNAME                                      DEPTNO
---------- ---------------------------------------- ----------
6 李四 6
7 王五 7 已用时间: 00: 00: 00.00

EXISTS反连接:

SQL:

select emp.* from emp where not exists  (select NULL from dept where dept.deptid=emp.deptno)

查询结果:

SQL> select emp.* from emp where not exists  (select NULL from dept where dept.deptid=emp.deptno);

     EMPID EMPNAME                                      DEPTNO
---------- ---------------------------------------- ----------
6 李四 6
7 王五 7 已用时间: 00: 00: 00.00

参考网页:https://docs.oracle.com/cd/E11882_01/server.112/e41084/queries006.htm#SQLRF30046

实验环境:

# 类别 版本
1 操作系统 Win10
2 数据库 Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
3 硬件环境 T440p
4 内存 8G

--2020年2月6日--

两表连接各种Join图示,SQL及查询结果的更多相关文章

  1. 9、SQL基础整理(两表连接exists,join on,union)

    exists的用法 select *from haha where exists (select *from bumen where bumen.code = haha.bumen and bumen ...

  2. sql表连接left join,right join,inner join三者之间的区别

    sql表连接left join,right join,inner join区别 left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 (以左表数据为基准,不足补为NULL) ...

  3. {MySQL的逻辑查询语句的执行顺序}一 SELECT语句关键字的定义顺序 二 SELECT语句关键字的执行顺序 三 准备表和数据 四 准备SQL逻辑查询测试语句 五 执行顺序分析

    MySQL的逻辑查询语句的执行顺序 阅读目录 一 SELECT语句关键字的定义顺序 二 SELECT语句关键字的执行顺序 三 准备表和数据 四 准备SQL逻辑查询测试语句 五 执行顺序分析 一 SEL ...

  4. oracle 表连接 - hash join 哈希连接

    一. hash 连接(哈希连接)原理 指的是两个表连接时, 先利用两表中记录较少的表在内存中建立 hash 表, 然后扫描记录较多的表并探測 hash 表, 找出与 hash 表相匹配的行来得到结果集 ...

  5. hibernate两表连接查询

    1.两表的关联关系为一对一 2.库存表Stock与商品信息表Product 3.库存表查询商品表里的商品名称,商品编号 库存表字段:    private String id;    private ...

  6. SqlServer 多表连接、聚合函数、模糊查询、分组查询应用总结(回归基础)

    --exists 结合 if else 以及 where 条件来使用判断是否有数据满足条件 select * from Class where Name like '%[1-3]班' if (not ...

  7. sql两表连接

    一直以来认为exists比in效率高的说法是不准确的.如果查询的两个表大小相当,那么用in和exists差别不大.如果两个表中一个较小,一个是大表,则子查询表大的用exists,子查询表小的用in:例 ...

  8. 从两表连接看Oracle sql优化器的效果

    select emp.*,dept.* from tb_emp03 emp,tb_dept03 dept where emp.deptno=dept.id -- 不加hint SQL> sele ...

  9. c# DataTable join 两表连接

    转:https://www.cnblogs.com/xuxiaona/p/4000344.html JlrInfodt和dtsource是两个datatable,通过[姓名]和[lqry]进行关联 v ...

随机推荐

  1. Python 面向对象之高级编程

    7.面向对象高级编程 7.1使用__slots__ python动态语言,new 对象后绑定属性和方法 Tip:给一个实例绑定的方法,对其他对象无效.可以通过对class绑定后,所有对象可以调用该方法 ...

  2. Redis持久化存储——>RDB & AOF

    Redis中两种持久化存储机制RDB和AOF redis是一个内存数据库,数据保存在内存中,但是我们都知道内存的数据变化是很快的,也容易发生丢失.幸好Redis还为我们提供了持久化的机制,分别是RDB ...

  3. 金题大战Vol.0 A、凉宫春日的叹息

    金题大战Vol.0 A.凉宫春日的叹息 题目描述 给定一个数组,将其所有子区间的和从小到大排序,求第 \(k\) 小的是多少. 输入格式 第一行两个数\(n\),$ k\(,表示数组的长度和\)k$: ...

  4. 编译原理LR(0)项目集规范族的构造详解

    转载于https://blog.csdn.net/johan_joe_king/article/details/79051993#comments 学编译原理的时候,感觉什么LL(1).LR(0).S ...

  5. 虚拟化技术之kvm虚拟机创建工具qemu-kvm

    在前边的博客中我们介绍了如何创建kvm虚拟机,以及一些常用的工具的介绍和使用,今天我们来了解下kvm原始工具qemu-kvm:为什么说qemu-kvm是一个原始的工具呢,如果你用kvm虚拟机,心细的你 ...

  6. failed to find romfile "efi-virtio.rom"

    问题:failed to find romfile "efi-virtio.rom" 解决:apt-get install ipxe-qemu

  7. Spring Security拦截器加载流程分析--练气中期

    写在前面 上回我们讲了spring security整合spring springmvc的流程,并且知道了spring security是通过过滤器链来进行认证授权操作的.今天我们来分析一下sprin ...

  8. Alpha阶段项目复审(小菜鸡联盟)

    Alpha项目复审 小队:小菜鸡联盟 团队名称 项目名称 评价 排名 『S.L.N』 OnTime 优点:团队分工合理明确,每个成员有一定的开发经验,能用到自己较为熟悉的技术进行开发:在开发初期制定了 ...

  9. Selenium的WebDriver API元素定位中的XPath和CSS

    元素的定位和操作是自动化测试的核心部分,其中操作又是建立在定位的基础上的. 浏览器的常规操作 import time from selenium import webdriver # 打开浏览器 dr ...

  10. Windows中使用PowerShell查看和卸载补丁

    查看:get-hotfix -id KB4470788 卸载:wusa /uninstall /kb:3045999 get-hotfix -id KB4470788 wusa /uninstall ...