Oracle关于left join on…and 及 left join on...whe...

 关于left join...on...and...以及left join...on...where的区别,网上不少的说法是对于left join...on...and...,and所限定的条件只对关联字段起做用,好比select a.* from tmp_table_a a left join tmp_table_b b on a.col1=b.col1 and b.col2=xx,不少人认为条件b.col2=xx是不起做用的。 测试

  对于这种说法,我我的是不认同的,至少来说,这是一种不负责任的说法。如下是个人一些测试例子,从这里你们就能够理解这两个用法的区别。 spa

select count(*) from tb_bo_valusr_new where month=201010 and brand=3;--141858
select count(*) from tb_bo_valusr_new where month=201010;---2281189
select count(distinct usr_nbr) from tb_bo_valusr_new where brand=3;--152110
select count(distinct usr_nbr) from tb_bo_valusr_new where month=201010;--2281189
select count(*) from tb_bo_valusr_new;--4602747
select count(*) from tmp_msy_bj_001;--986843
select count(*) from tmp_msy_bj_001 where if_wlg='是';--272623
table

--例1
create table tmp_msy_bj_007
as select
a.usr_nbr,b.cmcc_branch_cd from
tmp_msy_bj_001 a
left join tb_bo_valusr_new b on a.usr_nbr=b.usr_nbr;
select count(*) from tmp_msy_bj_007;--1957872
select count(distinct usr_nbr) from tmp_msy_bj_007;---986843
select count(*) from (select distinct * from tmp_msy_bj_007);--1024792
select

--例2 tab

create table tmp_msy_bj_008
as select
a.usr_nbr,b.cmcc_branch_cd from
tmp_msy_bj_001 a
left join tb_bo_valusr_new b on a.usr_nbr=b.usr_nbr and b.month=201010;
select count(*) from tmp_msy_bj_008;--986843
select count(distinct usr_nbr) from tmp_msy_bj_008;--986843
select count(*) from (select distinct * from tmp_msy_bj_008);--986843
di

--例3 时间

create table tmp_msy_bj_006
as select
a.usr_nbr,b.cmcc_branch_cd from
tmp_msy_bj_001 a
left join tb_bo_valusr_new b on a.usr_nbr=b.usr_nbr and b.month=201010
where a.if_wlg='是';
select count(*) from tmp_msy_bj_006;--272623
select count(distinct usr_nbr) from tmp_msy_bj_006;--272623
select count(*) from (select distinct * from tmp_msy_bj_006);--272623
co

--例4 join

create table tmp_msy_bj_005
as select
a.usr_nbr,b.cmcc_branch_cd from
tmp_msy_bj_001 a
left join tb_bo_valusr_new b on a.usr_nbr=b.usr_nbr and b.month=201010 and b.brand=3;
select count(*) from tmp_msy_bj_005;--986843
select count(distinct usr_nbr) from tmp_msy_bj_005;--986843
select count(*) from(select distinct * from tmp_msy_bj_005);--986843
new

--例5

create table tmp_msy_bj_003
as select
a.usr_nbr,b.cmcc_branch_cd,b.brand from
tmp_msy_bj_001 a
left join tb_bo_valusr_new b on a.usr_nbr=b.usr_nbr and b.brand=3;
select count(*) from tmp_msy_bj_003;--1062507
select count(distinct usr_nbr) from tmp_msy_bj_003;--986843
select count(*) from (select distinct * from tmp_msy_bj_003);--991560
select count(*) from tmp_msy_bj_003 where brand=3;--154124
select count(distinct usr_nbr) from tmp_msy_bj_003 where brand=3;--78460
select count(*) from tmp_msy_bj_003 where brand is null;--908383

--例6

create table tmp_msy_bj_002
as select
a.usr_nbr,b.cmcc_branch_cd from
tmp_msy_bj_001 a
left join tb_bo_valusr_new b on a.usr_nbr=b.usr_nbr where b.brand=3;
select count(*) from tmp_msy_bj_002;---154124
select count(distinct usr_nbr) from tmp_msy_bj_002;--78460
select count(*) from(select distinct * from tmp_msy_bj_002);--83177

--例7

create table tmp_msy_bj_011
as select
a.usr_nbr,b.cmcc_branch_cd from
tmp_msy_bj_001 a,tb_bo_valusr_new b where a.usr_nbr=b.usr_nbr and b.brand=3;
select count(*) from tmp_msy_bj_011;---154124
select count(distinct usr_nbr) from tmp_msy_bj_011;--78460
select count(*) from(select distinct * from tmp_msy_bj_011);--83177

--例8

create table tmp_msy_bj_009
as select
a.usr_nbr,b.cmcc_branch_cd from
tmp_msy_bj_001 a
left join tb_bo_valusr_new b on a.usr_nbr=b.usr_nbr where b.brand=3 and b.month=201010;
select count(*) from tmp_msy_bj_009;---78421
select count(distinct usr_nbr) from tmp_msy_bj_009;--78421
select count(*) from(select distinct * from tmp_msy_bj_009);--78421
--以上各个表的建立时间都在20秒内完成

--例9

create table tmp_msy_bj_004
as select
a.usr_nbr,b.cmcc_branch_cd from
tmp_msy_bj_001 a
left join tb_bo_valusr_new b on a.usr_nbr=b.usr_nbr and a.if_wlg='是';
--运行25分钟都尚未结果

在以上本例子中,b表tb_bo_valusr_new表中每月都包含当月全量号码,且单月号码是没重复的,而a 表tmp_msy_bj_001没有重复记录。在这样的例子中,若是在left join 以后用 where 指定 b的非链接字段的限制条件,已至关于将A与B等值链接了,很明显,这时的left join已失去意义,left join的本义是保留前面表的全部记录,所以说将后一表中非链接字段的限制条件放在where以后是一种愚蠢的作法。而从上面的例5还能够看出,在left join 后指定and b.column=....起的效果是:将a中符合b条件的记录链接,并保留a中其它不符合的记录一次。所以能够这么理解left join ...on...and...:能够保证保留前面表的所有记录,并可取到后面表中符合指定条件的记录。而出现后面表的and...条件不起做用的状况是:后一表即b表的记录集是a表的一个子集。

 再从例9中看出,绝对不要把a的限制条件放在left join on...and...指定,那是找死。

相关文章
相关标签/搜索