1.左连接(left join)
例如:select * from a left join b on (a.id= b.id and a.level = '1' ) =========>a 表数据全部显示
2.右连接(right join)
例如:select * from a right join b on (a.id= b.id and a.level = '1') =========>b 表数据全部显示
3.全表连接(full join)
例如:select * from a full join b on (a.id= b.id and a.level = '1') =========>a和b 表数据全部显示
4.(+)的用法(+放在哪边,表示左或右连接)
例如:select * from a, b where a.id= b.id(+) =====>a表的数据全部显示;
select * from a, b where a.id(+)= b.id =====>b表的数据全部显示.
5.(,)逗号连接:表示等值连接
例如:select * from a, b where a.id= b.id =======>a表与b表匹配上的才能查出来
连接总结:
1-4:都是非等值连接,只有5是等值连接
6.表连接的on与where的用法
1)on : 两个表通过on 处的条件连接成新的表
2) where : 针对from 形成的新表,对新表进行条件筛选
3) 例如 :select * from a left join b on (a.id= b.id and a.level = '1' and b.level = '2') where a.name = 'lily';
a和b通过on条件形成新的临时表(ab),where后的条件是针对临时表ab作进一步的限制。
7. 等值树状结构与非等值的树状接口
1)等值树状结构
select distinct s1.FREIGHT_AREA_NAME 总部名称, s1.FREIGHT_AREA_CODE 总部编码,
s2.FREIGHT_AREA_NAME 省份名称, s2.FREIGHT_AREA_CODE 省份编码,
s3.FREIGHT_AREA_NAME 市名称, s3.FREIGHT_AREA_CODE 市编码,
s4.FREIGHT_AREA_NAME 区县名称, s4.FREIGHT_AREA_CODE 区县编码
from FREIGHT_AREA s1, FREIGHT_AREA s2,
FREIGHT_AREA s3, FREIGHT_AREA s4
where s1.freight_area_code = s2.parent_area_code
and s2.freight_area_code = s3.parent_area_code
and s3.freight_area_code = s4.parent_area_code
and s1.REC_STATUS = '1' and s2.REC_STATUS = '1' and s3.REC_STATUS = '1' and s4.REC_STATUS = '1'
and s1.AREA_LEVEL in ('1')
and s2.AREA_LEVEL in ('2')
and s3.AREA_LEVEL in ('3')
and s4.AREA_LEVEL in ('4')
order by s1.FREIGHT_AREA_CODE, s2.FREIGHT_AREA_CODE, s3.FREIGHT_AREA_CODE, s4.FREIGHT_AREA_CODE
2)非等值的树状接口
select distinct s2.cname 省名称
s2.ts_bm 省编码
s3.cname 市名称
s3.ts_bm 市编码
s4.cname 区名称,
s4.ts_bm 区编码
from system_city s1
right join system_city s2 on (s1.cname = s2.upcname and s1.ts_level = '0' and s2.ts_level = '1')
full join system_city s3 on (s2.ts_bm = s3.ts_up_bm and s3.ts_level = '2')
left join system_city s4 on (s3.ts_bm = s4.ts_up_bm and s4.ts_level = '3')
where s2.ts_level = '1'
order by s2.ts_bm, s3.ts_bm, s4.ts_bm
3)start with connect by prior(专门用于查询树状结构)
层次化查询,即树型结构查询,是SQL中经常用到的功能之一,通常由根节点,父节点,子节点,叶节点组成,其语法如下:
SELECT [LEVEL] ,column,expression,...
FROM table_name
[WHERE where_clause]
[[START WITH start_condition] [CONNECT BY PRIOR prior_condition]];
LEVEL:伪列,用于表示树的层次
start_condition:层次化查询的起始条件,指定阶层的根。
prior_condition:定义父节点和子节点之间的关系,PRIOR指定父节点。作为运算符,PRIOR和加(+)减(-)运算的优先级相同。condition ... PRIOR expr = expr 或者 ... expr = PRIOR expr
例如:
CONNECT BY last_name != 'King' AND PRIOR employee_id = manager_id ...
CONNECT BY PRIOR employee_id = manager_id and PRIOR account_mgr_id = customer_id SYS_CONNECT_BY_PATH
SYS_CONNECT_BY_PATH这个函数是oracle9i才新提出来的!它一定要和connect by子句合用!第一个参数是形成树形式的字段,第二个参数是父级和其子级分隔显示用的分隔符!
(一)当前节点遍历子节点:
select
dp_code,
max
(sys_connect_by_path(dp_code,
'->'
)),
level
from
dp_mstr
where
1=1
--and dp_code = '商用结构部'
--and level > 1
start
with
dp_code=
'商用科技公司'
connect
by
prior
dp_code = dp_upper_dept
group
by
dp_code,
level
order
by
level
desc
;
(二)当前节点遍历根节点:
select
dp_code,
max
(sys_connect_by_path(dp_code,
'->'
)),
level
from
dp_mstr
where
1=1
--and dp_code = '商用结构部'
--and level > 1
start
with
dp_code=
'商用科技公司'
connect
by
prior
dp_upper_dept = dp_code
group
by
dp_code,
level
order
by
level
desc
;
总结:
若当前节点遍历子节点,则prior应该放在子节点一侧;
若当前节点遍历根节点,则prior应该放在上级节点一侧。