Oracle 中的join

1、概述

1.1、所有的join连接,都可以加上类似where a.id='1000'的条件,达到同样的效果。

1.2、除了cross join不可以加on外,其它join连接都必须加上on关键字,后都可加where条件。

1.3、虽然都可以加where条件,但是他们只在标准连接的结果集上查找where条件。比如左外连接的结果没有class的三班,所以如果加 where class.id='C003'虽然在表中有,但在左连接结果集中没有,所以查询后,是没有记录的。

2、实例,标准的join连接,(不加where条件的)

2.1、设有表如下:

学生表

班级表,对应学生表中的classid

2.2、自连接:join ,inner join

1 --自连接  :只返回两张表连接列的匹配项。2 --以下三种查询结果一样。3 select * from student s inner join class c on s.classid=c.id; 4 select * from student s join class c on s.classid=c.id;5 select * from student s,class c where s.classid=c.id;

自连接结果:

2.3、笛卡儿乘积:cross join

1 --笛卡儿乘积连接 :即不加任何条件,达到 M*N 的结果集。2 --以下两种查询结果一样。3 select * from student s cross join class c;4 select * from student,class;

笛卡尔结果:

注意:如果cross join加上where s.classid=c.id条件,会产生跟自连接一样的结果:

1 --加上条件,产生跟自连接一样的结果。2 select * from student s cross join class c where s.classid=c.id;

自连接结果集的cross join连接结果

2.3、左外连接:left join

1 --左连接 :列出左边表全部的,及右边表符合条件的,不符合条件的以空值代替。2 --在(+)计算时,哪个带(+)哪个需要条件符合的,另一个全部的。即放左即右连接,放右即左连接。3 --以下结果集相同。4 select * from student s left join class c on s.classid=c.id;5 select * from student s,class c where s.classid=c.id(+);

左连接结果:

2.4、右外连接:right join

1 --右外连接 :与左连接一样,列出右边表全部的,及左边表符合条件的,不符合条件2 --的用 空值  替代。3 --(+)一样,它的位置与连接相反。4 select * from student s right join class c on s.classid=c.id;5 select * from student s,class c where s.classid(+)=c.id;

右连接结果

2.5、全连接:full join

1 --全连接 :产生M+N的结果集,列出两表全部的,不符合条件的,以空值代替。2 select * from student s full join class c on s.classid=c.id;

全连接结果集

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

MySQL查询之内连接,外连接查询场景的区别与不同

前言我在写sql查询的时候,用的最多的就是where条件查询,这种查询也叫内连查询innerjoin,当然还有外连查询outerjoin,左外连接,右外连接查询,常用在多对多关系中,那他们区别和联系是什么呢?内连接innerjoin内连接最常用定义:1.连接结果仅包含符合连接条件的行组合起来作为结果集,参与连接的两个表都应该符合连接条件使用关键词:

oracle树状查询

connectby是结构化查询中用到的,其基本语法是:select...fromtablenamestartwith条件1connectby条件2where条件3;例:select\fromtablestartwithorg\_id'HBHqfWGWPy'conne