oracle查询以 连接,Oracle中连接查询怎么运用??

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

0a764d85ed180bf7df1235a8f6e14b1c.pngselect * 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;

四. 左外连接

--需求:查询业主的账务记录,显示业主编号、名称、年、月、金额。如果此业主没有账务记录也要列出姓名。

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(+);

五.右外连接查询

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

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.字段

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

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

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

Oracle中连接查询怎么运用??相关教程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值