full join关联字段都含null值报错

今天在测试full join的时候意外发现一个报错

with t1 as (
  select 
  null as col1,null as col2,null as col3
)
,t2 as (
   select 
   1 as col1,null as col2,null as col3 
)
select 
t1.*,t2.*
from t1 
full join t2 
on t1.col1 = t2.col1
and t1.col2 = t2.col2
and t1.col3 = t2.col3

报错:

org.apache.impala.common.NotImplementedException: Error generating a valid execution plan for this query. A FULL OUTER JOIN type with no equi-join predicates can only be executed with a single node plan.

一开始没看懂什么原因,我猜测是上面例子关联出来的数据都是Null值,才会报错,后来我就多加了一行t1.col1=1的数据进行测试,这样就可以保证关联字段值不都是null了,如下:

with t1 as (
  select 
  null as col1,2 as col2,null as col3,
  1    as col1,2 as col2,null as col3
)
,t2 as (
   select 
   1 as col1,null as col2   ,null as col3
  --  1 as col1,2    as col2   ,null as col3 
)
select 
t1.*,t2.*
from t1 
full join t2 
on t1.col1 = t2.col1
and t1.col2 = t2.col2
and t1.col3 = t2.col3

结果还是一样,暂时结论是只有保证两个表中的col1或col2或col3中的至少一个字段的所有值不为null才不会报错,如下:

with t1 as (
  select 
  1 as col1,2 as col2,null as col3
)
,t2 as (
   select 
   1 as col1,null as col2   ,null as col3
  --  1 as col1,2    as col2   ,null as col3 
)
select 
t1.*,t2.*
from t1 
full join t2 
on t1.col1 = t2.col1
and t1.col2 = t2.col2
and t1.col3 = t2.col3

希望有大佬知道的可以指出具体原因;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 MySQL 中,没有直接支持 FULL OUTER JOIN 的语法,因此执行 `SELECT * FROM t1 FULL OUTER JOIN t2` 会报错。如果你需要进行 FULL OUTER JOIN 操作,可以使用以下两种方式之一: 1. 使用 UNION 和 LEFT/RIGHT JOIN 实现 FULL OUTER JOIN: ``` SELECT * FROM t1 LEFT JOIN t2 ON t1.id = t2.id UNION SELECT * FROM t1 RIGHT JOIN t2 ON t1.id = t2.id WHERE t1.id IS NULL; ``` 这个语句将会将 `t1` 和 `t2` 按照 `id` 字段进行 FULL OUTER JOIN,即会返回 `t1` 和 `t2` 中所有的行。首先,使用 LEFT JOIN 将 `t1` 和 `t2` 进行连接,并将其中匹配成功的行返回。然后,使用 RIGHT JOIN 将 `t2` 和 `t1` 进行连接,并将其中未匹配成功的行返回。最后,将两个结果集使用 UNION 进行合并即可。 2. 使用子查询和 UNION 实现 FULL OUTER JOIN: ``` SELECT COALESCE(t1.id, t2.id) AS id, t1.name, t2.age FROM t1 LEFT JOIN t2 ON t1.id = t2.id UNION SELECT COALESCE(t1.id, t2.id) AS id, t1.name, t2.age FROM t1 RIGHT JOIN t2 ON t1.id = t2.id WHERE t1.id IS NULL; ``` 这个语句与第一种方式类似,只是使用了子查询来简化语法。首先,使用 LEFT JOIN 将 `t1` 和 `t2` 进行连接,并将其中匹配成功的行返回。然后,使用 RIGHT JOIN 将 `t2` 和 `t1` 进行连接,并将其中未匹配成功的行返回。最后,使用 COALESCE 函数来处理 `id` 字段NULL ,将两个结果集使用 UNION 进行合并即可。 需要注意的是,在使用 FULL OUTER JOIN 时,应该尽可能地避免使用 SELECT * 的语法,而是应该明确指定需要查询的列。这样做可以减少数据的冗余,避免不必要的计算和存储,提高查询效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值