当前位置: 首页 > news >正文

oracle 基础知识之 多表查询

多表查询定义:当查询的数据并不是来源一个表时,需要使用多表连接操作完成查询。多表连接查询通过表之间的关联字段,一次查询出多个表的数据。多表查询包括了等值连接、左连接、右连接、完全连接。
1.等值连接
等值连接也称为简单连接(simple joins)或者内连接(inner join)。通过等号来判断连接条件中的数据值是否相匹配,具体格式如下:
Select table1_name.fieldname,
table2_name.fieldname
from table1_name, table2_name
Where 条件句

【例1】建立雇员表,包括字段编号,姓名,岗位名称。建立职工薪水表包括职工号,姓名,工资。需要查询出编号,姓名,岗位名称,薪水信息。具体代码如下:
create table EMPLOYEE
( id_1 VARCHAR2(20),
name_1 VARCHAR2(25),
station VARCHAR2(20)
)
create table SALARY
(
id_2 VARCHAR2(20),
name_2 VARCHAR2(25),
salary NUMBER(8,2)
)
insert into employee values(‘1’,‘lily’,‘工程师’);
insert into employee values(‘3’,‘lucy’,‘项目经理’);
insert into employee values(‘5’,‘william’,‘人事经理’);
insert into employee values(‘6’,‘demon’,‘行政人员’);
insert into employee values(‘7’,‘david’,‘人事专员’);
Commit;
insert into salary values(‘1’,‘lily’,5000);
insert into salary values(‘2’,‘tom’,8000);
insert into salary values(‘3’,‘lucy’,5500);
insert into salary values(‘4’,‘linda’,3500);
insert into salary values(‘5’,‘william’,6000);
Commit;

select a.id_1,a.name_1,a.station,b.salary
from employee a,salary b
where a.id_1=b.id_2
–也可以使用关键字inner join
select a.id_1,a.name_1,a.station,b.salary
from employee a
inner join salary b
on a.id_1=b.id_2

查询结果如图3-7所示:
在这里插入图片描述

图使用内连接的查询结果

从查询结果上可以看出查询了雇员表,薪水表中编号相同的数据。

2.外连接
(1)左外连接:使用关键字 left join,以左表为准,返回左表的所有数据,右表满足关联条件的数据会全部显示,否则用 null 值去填充。
【例2】使用左连接查询出编号,姓名,岗位名称,薪水信息,代码如下:
select a.id_1,a.name_1,a.station,b.salary
from employee a
Left join salary b
on a.id_1=b.id_2 and a.name_1=b.name_2
查询结果如图3-8所示:

在这里插入图片描述

图3-8使用左连接的查询结果
该查询的主表是employee,查询记录数与employee 一致,职工号全部是employee 数据。
(2)右外连接:和左外连接相反。使用关键 right join,以右表为准,返回右表的所有数据,左表满足 on 条件的数据会全部显示,否则用 null 值去填充。
【例3-5】使用右连接查询出编号,姓名,岗位名称,薪水信息,代码如下:

select a.id_1,a.name_1,a.station,b.salary
from employee a
right join salary b
on a.id_1=b.id_2 and a.name_1=b.name_2
查询结果如图3-9下:

图3-9使用右连接的查询结果
(2)完全连接:通过关键字 full join 连接两张表,返回左表和右表的所有数据,并使用 null 值填充缺失的数据。
【例3-6】使用完全连接查询出编号,姓名,岗位名称,薪水信息,代码如下:
select a.id_1,a.name_1,b.id_2,b.name_2,a.station,b.salary
from employee a
full join salary b
on a.id_1=b.id_2 and a.name_1=b.name_2
查询结果如图3-10所示:
在这里插入图片描述

图3-10使用完全连接的查询结果
最后结果为 表A,表b的所有数据。

相关文章:

  • soul能用ip精准定位吗?ip属地准确吗
  • hot100算法刷题:二叉树的层序遍历
  • Spring Boot 核心知识点深度详解:自动化配置 (Auto-configuration) - 解锁 Spring Boot 的 “魔法”
  • 远程访问家里电脑上部署的Stable diffusion - 免费篇
  • error: Microsoft Visual C++ 14.0 or greater is required解决不需要安装到c盘等
  • Mybatis使用Druid连接池
  • 0x04.若依框架微服务开发(含AI模块运行)
  • Kotlin 中 let 方法的作用和使用场景
  • 人工智能助力家庭机器人:从清洁到陪伴的智能转型
  • ngx_http_conf_ctx_t
  • 【源码分析】Nacos实例注册流程分析-事件驱动框架
  • react实现虚拟列表
  • C#语法基础总结
  • C语言刷题第六章(下)
  • 神策数据接入 DeepSeek,AI 赋能数据分析与智能运营
  • 【华为OD-E卷 -122 字符统计及重排 100分(python、java、c++、js、c)】
  • repo init 错误 Permission denied (publickey)
  • 算法刷题记录——LeetCode篇(4) [第301~400题](持续更新)
  • CI/CD构建与注意事项
  • Vue3-高级特性
  • 近七成科创板公司2024年营收增长,285家营收创历史新高
  • 原国家有色金属工业局副局长黄春萼逝世,享年86岁
  • 外交部回应涉长江和记出售巴拿马运河港口交易:望有关各方审慎行事,充分沟通
  • 央行副行长:增强外汇市场韧性,坚决对市场顺周期行为进行纠偏
  • 商务部:将积极会同相关部门加快推进离境退税政策落实落地
  • 视频丨伊朗港口爆炸事件灭火工作已完成80%