【问题标题】:SQL - How to join two tables but where only certain things are exact match?SQL - 如何连接两个表,但只有某些东西是完全匹配的?
【发布时间】:2021-02-27 17:57:03
【问题描述】:

我不知道这是否可能,或者它的多对多连接情况很复杂,但这是我正在寻求帮助的情况:

Table1:
Name  Value1 Value2 Value3
John  Demo   DAT    12.5
Jane  Dawn   RAN    13.0

Table2:
Name  Value1 Value2 Value3
LOTS OF DATA

基本上,我希望创建一个连接语句,其中表 1 中包含的任何内容都包含 value1、value2(完全),如果 value3 不包含在表 2 中,则输出结果。请记住,这里没有唯一键。

我尝试过类似下面的方法,但似乎没有用:

Select * from Table2 as t2 inner join Table1 as t1 on t2.value1 = t1.value1
where t2.value1 = t1.value1
and t2.value2 = t1.value2
and t2.value3 <> t1.value3

我也看过工会,但不是 100% 确定。任何帮助表示赞赏。我认为这很简单,我只是想拥有一个查找表(表 1),其中包含表 2 应正确包含的值,然后将 Value3 列中未正确设置的任何内容输出为输出,因此我们可以更正那些。

【问题讨论】:

    标签: sql


    【解决方案1】:

    同时使用not existsexists

    select t1.*
    from table1 t1
    where not exists (select 1
                      from table2 t2
                      where t2.value1 = t.value1 and
                            t2.value2 = t.value2 and
                            t2.value3 = t.value3
                     ) and
          exists (select 1
                  from table2 t2
                  where t2.value1 = t.value1 and
                        t2.value2 = t.value2
                 ) ;
       
    

    【讨论】:

      猜你喜欢
      • 2022-01-17
      • 1970-01-01
      • 2018-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-30
      • 2011-10-28
      • 2012-01-25
      相关资源
      最近更新 更多