CREATE TABLE #TableA
(
ID int,
NID varchar(25),
Email varchar(50)
)
CREATE TABLE #TableB
(
ID int,
NID varchar(25),
Email varchar(50)
)
INSERT INTO #TableA(ID, NID, Email) VALUES (1, 'bob', 'bob@asdf.com')
INSERT INTO #TableA(ID, NID, Email) VALUES (2, 'mike', 'mike@asdf.com')
INSERT INTO #TableA(ID, NID, Email) VALUES (3, 'dave', 'dave@asdf.com')
INSERT INTO #TableB(ID, NID, Email) VALUES (1, 'bob', 'bob@asdf.com')
INSERT INTO #TableB(ID, NID, Email) VALUES (2, 'mike', 'mike@asdf.com')
INSERT INTO #TableB(ID, NID, Email) VALUES (3, 'doug', 'dave@asdf.com')
SELECT
#TableB.ID,
#TableB.NID,
#TableB.Email
FROM
#TableB Inner Join #TableA
ON #TableA.ID = #TableB.ID WHERE #TableA.NID <> #TableB.NID
DROP TABLE #TableA
DROP TABLE #TableB
Результат:
3 Дуг Дэйв@asdf.com
В заключение просто соединение с условием WHERE
SELECT
TableB.ID,
TableB.NID,
TableB.Email
FROM
TableB
Inner Join
TableA
ON TableA.ID = TableB.ID WHERE TableA.NID <> TableB.NID