??在两表关联时候(加不加outer对结果无影响),关联时候需要注意on和where两个关键字后面的条件,这里针对left [outer] join和full [outer] join做下说明,希望能帮到您。
left [outer] join时候的on与where
??左表的独立条件(例如:日期分区)要放在where中,右表的独立条件(例如:日期分区)可以放在on中
select a.id,a.name,b.job,b.salary from zero_user a
left join zero_job b
on a.id = b.id
where a.user_partition='20210813';
select a.id,a.name,b.job,b.salary from zero_user a
left join zero_job b
on a.id = b.id and b.job_partition='bigdata';
full [outer] join时候的on与where
??full [outer] join比较特殊,左表的独立条件依然可以放在where之后,和left join一致,但是右表的独立条件需要在子查询中体现,否则结果就会变成笛卡尔积。
select a.id,a.name,b.job,b.salary from zero_user a
full outer join zero_job b
on a.id = b.id and b.job_partition='bigdata';
select a.id,a.name,b.job,b.salary from zero_user a
full outer join (
select id,job,salary from zero_job where job_partition='bigdata') b
on a.id = b.id ;
|