简体   繁体   中英

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:

 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.

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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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