查询将从两个表返回非常见的empId

时间:2017-01-07 16:33:24

标签: sql-server

我有两张表EmployeeEmployeeAddress。我希望有一个查询可以从两个表中返回非常见的empId

表的结构。

  • Employee:EmpID和EmpName
  • EmployeeAddress:EmpID和地址

3 个答案:

答案 0 :(得分:2)

NOT INUNION的组合应该

select empid
from employee
where empid not in (select empid from EmployeeAddress)
union select empid
from EmployeeAddress
where empid not in (select empid from employee)

答案 1 :(得分:2)

一种方法是使用FULL OUTER JOIN

SELECT COALESCE(e.EmpID, ea.EmpID) AS EmpID
FROM   Employee e
       FULL OUTER JOIN EmployeeAddress ea
         ON e.EmpID = ea.EmpID
WHERE  e.EmpID IS NULL
        OR ea.EmpID IS NULL; 

答案 2 :(得分:1)

Select * FROM (select empid from employee Except select empid from EmployeeAddress )A
UNION 
SELECT * FROM (select empid from EmployeeAddress Except select empid from employee)B