黄骅住房和城乡建设局网站,做篮球网站用的背景图,推广网信息发布平台,工商注册系统加班遇到一个SQL问题#xff0c;本想把别人的SQL改下成SparkSQL#xff0c;在YARN上运行#xff0c;然而数据一直对不上。
原SQL
⚠️说明#xff1a;a.id#xff0c;b.id没有空的#xff0c;数据1:1#xff0c;b.name可能存在空的
select a.id,b.id,b.name
from tab…加班遇到一个SQL问题本想把别人的SQL改下成SparkSQL在YARN上运行然而数据一直对不上。
原SQL
⚠️说明a.idb.id没有空的数据1:1b.name可能存在空的
select a.id,b.id,b.name
from table_a a
left join table_b b on a.id b.id and b.is_delete 0 and b.name is not null
where
exists(select 1 from table_b c where a.id c.id and c.is_delete 0
)
改后的
想法是既然exists过滤了为什么不直接inner join呢于是乎
select a.id,b.id,b.name
from table_a a
inner join table_b b on a.id b.id and b.is_delete 0
求助群友
问了下群友区别是我一直以为left join 后 b.name is not null并没什么用就没有带我问群友下面这两个SQL有什么区别
select *
from table_a a
left join table_b b on a.id b.id and b.is_delete 0
where
exists(select 1 from table_b c where a.id c.id and c.is_delete 0
)select *
from table_a a
inner join table_b b on a.id b.id and b.is_delete 0
群友问AI的结果 这样的回答不太对
自悟
然后仔细去品味这两个SQL有什么不同
-- 1
select a.id,b.id,b.name
from table_a a
left join table_b b on a.id b.id and b.is_delete 0 and b.name is not null
where
exists(select 1 from table_b c where a.id c.id and c.is_delete 0
)-- 2select a.id,b.id,b.name
from table_a a
inner join table_b b on a.id b.id and b.is_delete 0
数据table_a id 1 2 3
数据table_b id name id_delete 1 aa 0 2 bb 0 3 NULL 0 结论
1sql计算后的会剔除掉 table_a 不符合 a.id c.id and c.is_delete 0 条件的数据。加上 name is not null。最后的数据会出现这两类情况
a.id,null,null # name为nullb表全部为空
a.id,b.id,b.name # 全部有值 a.id b.id b.name 1 1 aa 2 2 bb 3 NULL NULL
2sql计算后中则会出现这两类情况
a.id,b.id,null
a.id,b.id,b.name # 全部有值 a.id b.id b.name 1 1 aa 2 2 bb 3 3 NULL
所以count的时候是没有问题的两个都可以如果是取具体的值有所区别。
拓展
如果a.id b.id 是1:n 呢? 数据table_a id 1 2 3 10
数据table_b id name id_delete 1 aa 0 1 NULL 0 2 bb 0 3 NULL 0
-- 1
select a.id,b.id,b.name
from table_a a
left join table_b b on a.id b.id and b.is_delete 0 and b.name is not null
where
exists(select 1 from table_b c where a.id c.id and c.is_delete 0
) a.id b.id b.name 1 1 aa 2 2 bb 3 NULL NULL
-- 2select a.id,b.id,b.name
from table_a a
inner join table_b b on a.id b.id and b.is_delete 0 a.id b.id b.name 1 1 aa 1 1 NULL 2 2 bb 3 3 NULL
所以如果count的时候1对n相对于1:1是有区别的。
最后
1菜就要学就要钻研。
2加班使人头疼头脑不灵光。
3具体问题具体分析。