【学亮开讲】Oracle内外连接查询20181119

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

--外连接查询
--需求:查询业主的账务记录,显示业主编号、名称、年、月、金额。如果此业主没有账务记录也要列出姓名
--sql1999写法
select ow.id 业主编号,ow.name 业主名称,ac.year 年,ac.month 月,ac.money 金额
from t_owners ow left join t_account ac
on ac.owneruuid = ow.id 
--Oracle写法
select ow.id 业主编号,ow.name 业主名称,ac.year 年,ac.month 月,ac.money 金额
from t_owners ow,t_account ac
where ow.id=ac.owneruuid(+)
--右外连接查询
--需求:查询业主的账务记录,显示业主编号、名称、年、月、金额。如果账务记录没有对应的业主信息,也要列出记录。
--sql1999写法
select ow.id 业主编号,ow.name 业主名称,ac.year 年,ac.month 月,ac.money 金额
from t_owners ow right join t_account ac
on ac.owneruuid = ow.id
--Oracle写法
select ow.id 业主编号,ow.name 业主名称,ac.year 年,ac.month 月,ac.money 金额
from t_owners ow,t_account ac
where ow.id(+)=ac.owneruuid