查找仅出现在两个查询之一的结果中的行

时间:2013-10-03 20:51:21

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

我有两个JOIN查询,它们提供Id的输出列。如何查找查询1返回但查询2未返回的所有Id值?

select Id from Table1 join Table2;

select Id from Table2 join Table3;

2 个答案:

答案 0 :(得分:6)

SELECT id FROM dbo.Table1 INNER JOIN dbo.Table2 ON ...
EXCEPT
SELECT id FROM dbo.Table2 INNER JOIN dbo.Table3 ON ...;

答案 1 :(得分:0)

试试这个:

select Id from Table1 join Table2
union
select Id from Table2 join Table3