代码之家  ›  专栏  ›  技术社区  ›  beta

SQL:如果join返回多个匹配项,则只删除一行

  •  1
  • beta  · 技术社区  · 6 年前

    我有一个SQL表,如下所示:

    col1   col2
    a      b
    b      a
    c      d
    d      c
    f      g
    

    如您所见,两列都有行 col1 col2 是倒置的。我的意思是第一行的值 a b 两列中的值和第2行中的值都在那里,但另一种情况则相反。

    我现在想删除这些对中的每一行。我不在乎删除了对的哪一边。因此,应该删除第1行和第3行或第2行和第4行。

    结果应如下所示:

    col1   col2
    a      b
    c      d
    f      g
    

    col1   col2
    b      a
    d      c
    f      g
    

    我通过以下查询实现了这一点:创建两个人工列,其中包含按排序顺序排列的值,然后应用 GROUP BY 但是我认为应该有一个更好的解决方案。

    DELETE t1
    FROM testtable t1
    INNER JOIN (
        SELECT CASE WHEN col1 < col2 THEN col1 ELSE col2 END AS first, 
        CASE WHEN col1 < col2 THEN col2 ELSE col1 END AS second
        FROM testtable
        GROUP BY CASE WHEN col1 < col2 THEN col1 ELSE col2 END, CASE WHEN col1 < col2 THEN col2 ELSE col1 END
        ) t2 ON t2.first = t1.col1 AND t2.second = t1.col2
    
    3 回复  |  直到 6 年前
        1
  •  2
  •   Yann G    6 年前

    我认为您可以通过向联接添加条件来简化查询:

        DELETE T1
        FROM #testable T1
            INNER JOIN #testable T2 ON T1.col1 = T2.col2 AND T1.col2 = T2.col1 AND T1.col1 > T1.col2
    
        2
  •  0
  •   Yogesh Sharma    6 年前

    你可以使用 exists 和; not exists :

    select t.*
    from testtable t 
    where exists (select 1 
                  from testtable t1 
                  where t1.col1 > t.col1 and t1.col1 = t.col2
                 ) or
          not exists (select 1 
                      from testtable t1 
                      where t1.col1 < t.col1 and t1.col1 = t.col2
                     );
    

    如果要删除不需要的记录,可以执行以下操作:

    delete t
    from testtable t 
    where not exists (select 1 
                      from testtable t1 
                      where t1.col1 > t.col1 and t1.col1 = t.col2
                     ) and
           exists (select 1 
                   from testtable t1 
                   where t1.col1 < t.col1 and t1.col1 = t.col2
                  );
    
        3
  •  0
  •   Gordon Linoff    6 年前

    假设没有实际的副本,我会这样做:

    delete t from testtable t
        where col1 > col2 and
              exists (select 1
                      from testtable t2
                      where t2.col1 = t.col2 and t2.col2 = t.col1
                     );
    

    也就是说,删除 col1 > col2 但仅当“paired”行已存在于表中时。