我有两张表Employee
和EmployeeAddress
。我希望有一个查询可以从两个表中返回非常见的empId
。
表的结构。
Employee
:EmpID和EmpName EmployeeAddress
:EmpID和地址答案 0 :(得分:2)
NOT IN
和UNION
的组合应该
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