Oracle自学笔记整理(三)
六、连接查询
6.1 内连接
6.1.1等值连接
在连接条件中使用等于号(=)运算符比较被连接列的列值,其查询结果中列出被连接表中的所有列。
eg:
select` `a.stuid,
``a.stuname,
``a.classno,
``b.classno,
``b.classname,
``b.monitorid,
``b.monitorname,
``b.classaddress
``from` `stuinfo a, class b
``where` `a.classno = b.classno; --等值连接
6.1.2 非等值连接
在连接条件使用除等于运算符以外的其它比较运算符比较被连接的列的列值,这些关系运算符包括>、>=、<=、!>、!<、<>
select` `a.stuid,
``a.stuname,
``a.classno,
``b.classno,
``b.classname,
``b.monitorid,
``b.monitorname,
``b.classaddress
``from` `stuinfo a, class b
``where` `a.classno > b.classno; --非等值连接
6.2 外连接
6.2.1 左连接
返回包括左表中的所有记录和右表中联结字段相等的记录
--左外连接(stuinfo表中数据都存在,stuinfo_2018不在stuinfo中存在的学生相关字段为null值)
select` `a.*, b.stuid, b.stuname
``from` `stuinfo a
``left` `join` `stuinfo_2018 b
``on` `a.stuid = b.stuid;
--左外连接(利用(+)在右边)另外一种写法
select a.*, b.stuid, b.stuname
from stuinfo a,stuinfo_2018 b
where a.stuid=b.stuid(+);
6.2.2 右连接
返回包括右表中的所有记录和左表中联结字段相等的记录
--右外连接(stuinfo_2018表中数据都存在,stuinfo不在stuinfo_2018存在的学生相关字段为null值)
select` `a.*, b.stuid, b.stuname
``from` `stuinfo a
``right` `join` `stuinfo_2018 b
``on` `a.stuid = b.stuid;
--右外连接(利用(+)在左边)另外一种写法
select` `a.*, b.stuid, b.stuname
``from` `stuinfo a,stuinfo_2018 b
``where` `a.stuid(+)=b.stuid;
6.2.3 全外连接
查询结果等于左外连接和右外连接的和
--(全外连接(stuinfo、stuinfo_2018表中数据都存在,
``--stuinfo不在stuinfo_2018存在的学生相关字段为null值,
``--stuinfo_2018不在stuinfo存在的学生相关字段为null值)
select` `a.*, b.stuid, b.stuname
``from` `stuinfo a
``full` `join` `stuinfo_2018 b
``on` `a.stuid = b.stuid;