加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sql – 比较两个表在HIVE中是否相等

发布时间:2020-12-12 08:56:17 所属栏目:MsSql教程 来源:网络整理
导读:我有两个表,table1和table2.每个都有相同的列: key,c1,c2,c3 我想检查这些表是否相互相等(它们具有相同的行).到目前为止,我有两个查询( =在HIVE中不相等): select count(*) from table1 t1 left outer join table2 t2on t1.key=t2.keywhere t2.key is null
我有两个表,table1和table2.每个都有相同的列:
key,c1,c2,c3

我想检查这些表是否相互相等(它们具有相同的行).到目前为止,我有两个查询(<> =在HIVE中不相等):

select count(*) from table1 t1 
left outer join table2 t2
on t1.key=t2.key
where t2.key is null or t1.c1<>t2.c1 or t1.c2<>t2.c2 or t1.c3<>t2.c3

select count(*) from table1 t1
left outer join table2 t2
on t1.key=t2.key and t1.c1=t2.c1 and t1.c2=t2.c2 and t1.c3=t2.c3
where t2.key is null

所以我的想法是,如果返回零计数,表格是相同的.但是,我得到第一个查询的零计数,第二个查询的非零计数.他们究竟有何不同?如果有更好的方法来检查这一点肯定让我知道.

解决方法

第一个排除了t1.c1,t1.c2,t1.c3,t2.c1,t2.c2或t2.c3为空的行.这意味着您有效地进行内部联接.

第二个将找到存在于t1但不存在于t2中的行.

要查找t2中但不存在于t1中的行,您可以执行完全外部联接.以下SQL假定所有列都是NOT NULL:

select count(*) from table1 t1
full outer join table2 t2
on t1.key=t2.key and t1.c1=t2.c1 and t1.c2=t2.c2 and t1.c3=t2.c3
where t1.key is null /* this condition matches rows that only exist in t2 */
   or t2.key is null /* this condition matches rows that only exist in t1 */

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读