Oracle:比较两个不同表中没有主键的字符串列以查找匹配/不匹配的字符串

Posted

技术标签:

【中文标题】Oracle:比较两个不同表中没有主键的字符串列以查找匹配/不匹配的字符串【英文标题】:Oracle: Comparing string columns from two different table without a primary key to find match/unmatched string 【发布时间】:2020-06-26 14:22:35 【问题描述】:

我有两个表,我们想比较这些表中的两个字符串列。这些表中没有与 where 子句匹配的主键。表格如下: 比较应该忽略:

    忽略字符串中任何位置的空格 2) 忽略区分大小写

输出需求:

【问题讨论】:

【参考方案1】:

您可以在连接条件中将full outer joinupperreplace 函数一起使用,如下所示:

Select t1.str, t2.str,
       Case when t1.str is not null and t2.str is not null then 'exist in both table'
             when t1.str is not null then 'missing in table2'
             Else 'missing in table1'
       End as differences
From table1 t1 full join table2 t2
  On upper(replace(t1.str,' ','') = upper(replace(t2.str,' ','')

【讨论】:

非常感谢 Tejash 再次提供帮助,但我已经尝试了 FULL OUTER JOIN 但不知道为什么我会返回一些值,这些值存在于两个表中,但在输出中显示表 2 中缺少.....不知道为什么? FULL OUTER Join 有什么问题吗..... 你能告诉我你得到的确切结果吗?【参考方案2】:

同样地;第 1 - 16 行的样本数据。您可能需要的查询从第 17 行开始。

SQL> with
  2  tab1 (col) as
  3    (select 'mg/kl'     from dual union all
  4     select 'k/mg'      from dual union all
  5     select 'l/g/kg'    from dual union all
  6     select 'umol_Ulp'  from dual union all
  7     select '10*12_/KG' from dual union all
  8     select 'a/L/G'     from dual
  9    ),
 10  tab2 (col) as
 11    (select '10*12_/kg' from dual union all
 12     select '1/L/G'     from dual union all
 13     select 'PG/7@R'    from dual union all
 14     select 'KG/CM/L'   from dual union all
 15     select 'l/g/kg'    from dual
 16    )
 17  select a.col, b.col,
 18    case when lower(a.col) = lower(b.col) then
 19              'exists in both tables ' ||
 20              case when a.col = b.col then 'and exact match'
 21                   else 'but different'
 22              end
 23         when a.col is null then 'missing in table 1'
 24         when b.col is null then 'missing in table 2'
 25    end differences
 26  from tab1 a full outer join tab2 b on lower(a.col) = lower(b.col);

COL       COL       DIFFERENCES
--------- --------- -------------------------------------
mg/kl               missing in table 2
k/mg                missing in table 2
l/g/kg    l/g/kg    exists in both tables and exact match
umol_Ulp            missing in table 2
10*12_/KG 10*12_/kg exists in both tables but different
a/L/G               missing in table 2
          KG/CM/L   missing in table 1
          PG/7@R    missing in table 1
          1/L/G     missing in table 1

9 rows selected.

SQL>

【讨论】:

我只是在验证查询,并将尽快将其标记为解决方案...... 给专家的一个问题: T1 和 T2 的位置在 FULL JOIN 中是否重要?我的意思是 "On t1.str = t2.str" VS "On t2.str = t1 .str" 返回相同的结果??? —— 没关系,你会得到同样的结果。【参考方案3】:

尝试以下 SQL 一次,看看是否有效。否则,您能否分享那些它说“在表 2 中缺失”的值,即使它在两个表中都存在?

Select t1.str, t2.str,
       Case when nvl(t1.str,'X') != 'X' and nvl(t2.str,'X') != 'X' then 'exist in both table'
             when nvl(t1.str,'X') != 'X' then 'missing in table2'
             Else 'missing in table1'
       End as differences
From table1 t1 full join table2 t2
  On upper(trim(t1.str)) = upper(trim(t2.str))

【讨论】:

感谢 Shantanu,我匹配的列表不正确。您的 FULL JOIN 解决方案有效......非常感谢...... ...... 给专家的一个问题: T1 和 T2 的位置在 FULL JOIN 中是否重要?我的意思是 "On t1.str = t2.str" VS "On t2.str = t1 .str" 返回相同的结果??? 没关系。你应该得到相同的结果。

以上是关于Oracle:比较两个不同表中没有主键的字符串列以查找匹配/不匹配的字符串的主要内容,如果未能解决你的问题,请参考以下文章

oracle 唯一索引,唯一约束,主键之间的联系

如何在条件与数字之间使用 Oracle 中的字符串列

增量导入没有 HDFS 主键的 Oracle 表

如何在没有主键的情况下从 mySQL 中的两个表中获取不常见的记录

如何从特定数据库中的所有表中检索不属于主键和外键的所有列

SQL:选择另一个表中没有复合主键的条目