简体   繁体   English

如何从表A中选择所有行,其中表B中的两个相关列与表A中的相关列相同

[英]How to select all rows from tableA where two correlating columns in tableB are identical to the ones in tableA

Here is an example row found in both tableA and TableB: 这是在tableA和TableB中都找到的示例行:

 col1   col2    col3
123  |  asdf  |  ddd

I was going to try to use a CTE To retrieve this value, but i have only got it to work with one column at a time. 我打算尝试使用CTE来检索此值,但是我一次只能使它与一列一起工作。

Here is what i have so far, but right now I am stumped. 这是我到目前为止的东西,但是现在我很沮丧。

    ;with cte as (
     SELECT  A.col1
          ,A.col2
      FROM tableA A
        )

    select col1, col2 from cte where col1, col2

    not in (select col1, col2 from FHU.dbo.HolidayCallers)

And just to reiterate i am expecting output to be the original sample row up top. 只是重申一下,我希望输出结果是原始的样本行。

Based on your question, you'd could use EXISTS to find rows where the related columns are in both tables. 根据您的问题,您可以使用EXISTS查找两个表中相关列所在的行。

SELECT a.ConversationID, a.SendDateUtc
    FROM tableA a
    WHERE EXISTS(SELECT NULL
                     FROM FHU.HolidayCallers hc
                     WHERE a.ConversationID = hc.ConversationID
                         AND a.SendDateUtc = hc.SendDateUtc);

BUT, based on the sample code you provided, it seems like you are looking for a NOT EXISTS condition: 但是,根据您提供的示例代码,您似乎正在寻找“不存在”条件:

SELECT a.ConversationID, a.SendDateUtc
    FROM tableA a
    WHERE NOT EXISTS(SELECT NULL
                         FROM FHU.HolidayCallers hc
                         WHERE a.ConversationID = hc.ConversationID
                             AND a.SendDateUtc = hc.SendDateUtc);

You can use an INNER JOIN to find records that are in both tables: 您可以使用INNER JOIN查找两个表中的记录:

SELECT a.ConversationID, a.SendDateUtc
FROM tableA AS a
INNER JOIN FHU.dbo.HolicayCallers AS hc ON a.ConversationId = hc.ConversationId
   AND a.SendDateUtc = hc.SendDateUtc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM