【问题标题】:SQL Server 2008 R2: Find rows where column2 value present in column1SQL Server 2008 R2:查找 column2 值存在于 column1 中的行
【发布时间】:2018-07-06 11:46:24
【问题描述】:

我想在 col1 中找到 col2 记录的条目。

表:

CREATE TABLE Test_Table
(
    Col1 int,
    Col2 int
);

参赛作品:

INSERT INTO Test_Table VALUES(111,112),
                              (112,113),
                              (114,115),
                              (116,117),
                              (117,118),
                              (118,119);

预期结果:

Col1    Col2
-------------
111     112
112     113
116     117
117     118
118     119

注意:记录 114,115 未显示,因为 115 不存在于 col1 中。

我的尝试:

WITH CTE1
AS
(
    SELECT Col1, Col2
    FROM Test_Table
),
CTE2
AS
(
    SELECT t.Col1, t.Col2
    FROM Test_Table t
    INNER JOIN CTE1 s1
    ON s1.Col2 = t.Col1
    OR s1.Col2 = t.Col2
)
SELECT DISTINCT * FROM CTE2;

但是获取所有记录。

【问题讨论】:

  • 113 也不存在于 col1 中,为什么它会出现在您的结果集中?
  • 尽管col1 中没有 113,为什么结果中有 (112, 113)?
  • @stickybit,但值112 存在,因此需要检索值112 的两行。
  • 你的逻辑怎么样? 112, 113 有 112 存在于 col1 中,正如 114 存在于 col1 中的 114,115。这两个有什么区别?
  • col1 的值(此处为 112)始终存在于 col1 中。这意味着您只需要所有记录。

标签: sql sql-server sql-server-2008-r2


【解决方案1】:

这可能也有效

select t.* 
from   Test_Table t
where  exists (select 1
               from   test_table t2
               where  t2.col1 = t.col2 or t2.Col2 = t.Col1
              )

【讨论】:

    【解决方案2】:

    我想这就是你想要的。

    select t.*
    from #test_table t
    where exists (select 1
                  from #test_table t2
                  where t2.col1 = t.col2
                 )
       or exists (select 1
                  from #test_table t3
                  where t3.col2 = t.col1
                 );
    

    【讨论】:

      【解决方案3】:

      我想你只是想要exists:

      select tt.*
      from test_table tt
      where exists (select 1
                    from test_table tt2
                    where tt2.col1 = tt.col2
                   );
      

      【讨论】:

        【解决方案4】:

        使用CROSS JOIN:

        select t1.* from test_table t1 CROSS JOIN test_table t2
        on t1.col1 = t2.col2 
        UNION 
        select t1.* from test_table t1 CROSS JOIN test_table t2
        on t1.col2 = t2.col1 
        

        【讨论】:

        • 需要为CROSS JOIN 添加WHERE 而不是ON。 ;)
        【解决方案5】:

        根据您的 cmets,我怀疑您希望 col1/col2 存在于另一行的 col1/col2 中

        select tt.*
        from test_table tt
        where exists (select 1
                      from test_table tt2
                      where   -- Ensure not same row
                            (tt2.col1 <> tt.col1 or tt2.col2 <> tt.col2)
                        and -- Ensure at least one match
                            (tt2.col1 = tt.col1 or tt2.col1 = tt.col2 or tt2.col2 = tt.col1 or tt2.col2 = tt.col2)
                     );
        

        【讨论】:

          【解决方案6】:
          select t1.* from Test_Table t1 left join Test_Table t2 on t2.Col1=t1.Col2 where t2.Col2 is not null
          union
          select t1.* from Test_Table t1 left join Test_Table t2 on t2.Col2=t1.Col1 where t2.Col1 is not null
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-02-20
            • 1970-01-01
            相关资源
            最近更新 更多