SQL检查A列中的数据是否存在于B列中

SQL Check If Data in Column A Exists in Column B

我有一名员工Table

EmployeeID | Name | EmployeeEmail | ManagerEmail


每个 ManagerEmail 都应该属于经理级别的员工,作为他们的主要电子邮件地址 EmployeeEmail

我想检查是否有 ManagerEmail 不属于任何员工。


我使用的 DBMS 是 Microsoft SQL Server Management Studio 2014。

我可以知道我该怎么做吗?

谢谢。

由于这是一个非常笼统的问题,所以答案也同样笼统。使用以下方法之一:

  • 不在
  • 不存在
  • 除了
  • 反加入

似乎想到了 none 个,您可能想要研究所有这些。在我看来,你至少应该学会如何应用前三个。

你似乎想要:

select e.*
from Employee e
where e.ManagerEmail is not null and
      not exists (select 1 
                  from Employee e1 
                  where e1.EmployeeEmail = e.ManagerEmail
                 );

编辑:根据 OP 的评论添加了额外的 WHERE 子句。

尝试以下查询-:

select ManagerEmail from YourTableName 
where ManagerEmail not in (select EmployeeEmail from YourTableName)

SQL 服务器