Oracle中连接查询怎么运用??「终于解决」

Oracle中连接查询怎么运用??「终于解决」welcome to 煌sir详解Oracle 篇章,让我们继续一起学习吧~~ 查询–连接查询 一. 基础语法 笛卡尔积:两个表乘积,所有的数据最大集(开发无用) select * from A …

Oracle中连接查询怎么运用??

welcome to 煌sir详解Oracle 篇章,让我们继续一起学习吧~~ 

查询–连接查询

一. 基础语法

笛卡尔积:两个表乘积,所有的数据最大集(开发无用)

select * from A , B;

 

内连接

  • 隐式内连接

select * from A , B where a.id = b.aid;

 

  • 显示内连接

select * from A
inner join B on a.id = b.aid;

 

  • 外链接
  • 左外连接:查询左表(A)所有数据,如果条件成立显示右边(B)的数据,否则显示null

select * from A
left outer join B on a.id = b.aid

 

  • 右外连接:查询右表(B)所有数据,如果条件成立显示左边(A)的数据,否则显示null

select * from A
right outer join B on a.id = b.aid

 

三:内连接查询实用:

--1查询显示业主编号,业主名称,业主类型名称
--隐式内连接
select ow.id 业主编号,ow.name 业主名称,ot.name 业主类型名称 from t_owners ow,t_ownertype ot 
where ow.ownertypeid=ot.id ;

--显示内连接
select ow.id 业主编号,ow.name 业主名称,ot.name 业主类型名称 from t_owners ow
inner join t_ownertype ot on ow.ownertypeid = ot.id ;




--2查询显示业主编号,业主名称、地址和业主类型


--隐式内连接
select ow.id as 编号,ow.name as 业主名称,a.name as 地址名称,ot.name as 业主类型名称
 from t_owners ow,t_ownertype ot ,t_address a
where ow.ownertypeid=ot.id and ow.addressid=a.id;


--显示内连接
select ow.id as 编号,ow.name as 业主名称,a.name as 地址名称,ot.name as 业主类型名称
 from t_owners ow inner join t_address a on ow.addressid=a.id 
inner join t_ownertype ot on ow.ownertypeid=ot.id;




--3查询显示业主编号、业主名称、地址、所属区域、业主分类
--隐式内连接
select ow.id 业主编号,ow.name 业主名称,a.name 地址,ar.name 所属区域,ot.name 业主分类 
from t_owners ow,t_ownertype ot, t_address a, t_area ar 
where ow.ownertypeid=ot.id and ow.addressid=a.id and a.areaid=ar.id ;


--显示内连接
select ow.id 业主编号,ow.name 业主名称,a.name 地址,ar.name 所属区域,ot.name 业主分类 
from t_owners ow inner join t_ownertype ot on ow.ownertypeid=ot.id
inner join t_address a on ow.addressid=a.id
inner join t_area ar on a.areaid=ar.id;





--4查询显示业主编号、业主名称、地址、所属区域、收费员、业主分类
--隐式内连接
select ow.id 业主编号,ow.name 业主名称,ad.name 地址,ar.name 所属区域, op.name 收费员,ot.name 业主分类
 from t_owners ow ,t_address ad,t_area ar ,t_operator op ,t_ownertype ot
where ow.addressid=ad.id and ad.areaid=ar.id and ad.operatorid=op.id   and ow.ownertypeid=ot.id ;


--显示内连接
select ow.id 业主编号,ow.name 业主名称,ad.name 地址,ar.name 所属区域, op.name 收费员,ot.name 业主分类
from t_owners ow inner join t_address ad on ow.addressid=ad.id
inner join t_area ar on ad.areaid=ar.id 
inner join t_operator op on ad.operatorid=op.id  
inner join t_ownertype ot on ow.ownertypeid=ot.id order by ow.id asc;

代码100分

 

 

四. 左外连接

代码100分--需求:查询业主的账务记录,显示业主编号、名称、年、月、金额。如果此业主没有账务记录也要列出姓名。
select ow.id 业主编号,ow.name 名称,ac.year 年,ac.month 月,ac.money 金额 
from t_owners ow left outer join t_account ac 
on ow.id=ac.ownerid;


select ow.id 业主编号,ow.name 名称,ac.year 年,ac.month 月,ac.money 金额 
from t_owners ow left join t_account ac on ow.id=ac.ownerid;

 

Oracle 左外连接特殊用法(右边用+)

在内连接基础上,使用(+) 转换 左外连接

--oracle 左外连接 特殊用法:
select ow.id 业主编号,ow.name 名称,ac.year 年,ac.month 月,ac.money 金额 from t_owners ow, t_account ac where ow.id=ac.ownerid(+);
 

 

 

五.右外连接查询

代码100分--需求:查询业主的账务记录,显示业主编号、名称、年、月、金额。如果账务记录没有对应的业主信息,也要列出记录


select ow.id 业主编号,ow.name 名称,ac.year 年,ac.month 月,ac.money 金额 from t_owners ow right join  t_account ac  on ow.id=ac.ownerid;

 

 

Oracle 右外连接特殊用法(左边用+)

--oracle特殊语法
select ow.id 业主编号,ow.name 名称,ac.year 年,ac.month 月,ac.money 金额   from t_owners ow ,t_account ac where ow.id(+) =ac.ownerid;

 

 

小结:

内连接:

  • 隐式内连接

  • select * from 表1, 表2, …. where 表1.字段 = 表2.字段 and 连接条件

 

  • 显示内连接

select * from 表1
inner join 表2 on 表1.字段 = 表2.字段
inner join 表3 on 连接条件
…..

 

  • 外链接
  • 左外连接:查询左表(表1)的所有数据,条件成立显示右表(表2)数据,条件不成立显示null

select * from 表1
left outer join 表2 on 表1.字段 = 表2.字段
….

 

  • 右外连接:查询右表(表2)的所有数据,条件成立显示左表(表1)数据,条件不成立显示null

select * from 表1
right outer join 表2 on 表1.字段 = 表2.字段
….

 

 

  • Oracle 外链接,将隐式内连接转换成对应外链接 (了解)

左外连接:

select * from 表1, 表2 
where 表1.字段 = 表2.字段(+)

 

右外连接:

select * from 表1, 表2 
where 表1.字段(+) = 表2.字段

 

 

 

看完恭喜你,又知道了一点点!!!

你知道的越多,不知道的越多! 

~感谢志同道合的你阅读,  你的支持是我学习的最大动力 ! 加油 ,陌生人一起努力,共勉!!

 

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/8210.html

(0)
上一篇 2023-03-05
下一篇 2023-03-05

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注