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