按: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. WebApi的创建,部署,Oauth身份认证(一)

    1.首先创建一个项目 2.选择Web API 3.创建一个空的控制器 4.控制器名称为MyApiController using System; using System.Collections.Ge ...

  2. 文件上传Upload 学习笔记

    整理完自己还有点晕,看来还是得找点靶场自己练习练习Orz 1:客户端JavaScript校验 Burp改包即可 2:服务端对Content-Type进行校验 猜测后,修改对应Content-Type字 ...

  3. 企业级Gitlab-ci实践

    前言 吐槽一波 2020年6月2号刚入职公司时,第一感觉是集群环境是个大坑!内网一套,公网一套.内网采用单节点Kubernetes,公网采用aliyun托管的X节点Kubernetes(还有节点是2C ...

  4. day6 函数

    1.关键字参数     给实参对应的形参   调用函数时 设置关键字参数,形参=实参,把实参固定给那个形参 2.元组的可变(不定长参数)的使用      可变参数可以接收任意数量的普通的形参,并且组包 ...

  5. elasticsearch java工具类

    docker运行elasticsearch docker pull elasticsearch:7.8.1 docker run -p 9200:9200 -p 9300:9300 -e " ...

  6. 一文说通Jwt、Session、Cooike区别

    JWT 全称是 JSON Web Token,是目前非常流行的跨域认证解决方案,在单点登录场景中经常使用到. 有些人觉得它非常好用,用了它之后就不用在服务端借助 redis 实现认证过程了,但是,还有 ...

  7. Goland远程连接Linux开发调试

    参考链接: https://blog.csdn.net/huwh_/article/details/77879152?utm_source=blogxgwz3 https://baijiahao.ba ...

  8. 0基础算法基础学算法 第八弹 递归进阶,dfs第一讲

    最近很有一段时间没有更新了,主要是因为我要去参加一个重要的考试----小升初!作为一个武汉的兢兢业业的小学生当然要去试一试我们那里最好的几个学校的考试了,总之因为很多的原因放了好久的鸽子,不过从今天开 ...

  9. 在vmware上MBR方式安装archLinux

    进入安装盘 设置好vmware的相关选项,进入下载好的系统中,显示如下 联网 输入以下命令,后面加&使其后台运行 dhcpcd & 然后ping一下检测是否联网 ping baidu. ...

  10. Reinforcement learning in artificial and biological systems

    郑重声明:原文参见标题,如有侵权,请联系作者,将会撤销发布! Abstract 在生物和人工系统的学习研究之间,已经有富有成果的概念和想法流.Bush and Mosteller,Rescorla a ...