且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 SQL 中查找 IsManager 的最简单方法

更新时间:2022-05-26 21:43:34

希望这只是一个演示示例,而不是您真正的表结构.

Hopefully this is just a demo example not your real table structure.

SELECT Employee,
       Manager,
       CASE
         WHEN EXISTS(SELECT *
                     FROM   Employee e2
                     WHERE  e2.Manager = e1.Employee) THEN 1
         ELSE 0
       END As IsAManager
FROM   Employee e1  

有关 SQL Server 如何处理 CASE 表达式中的 EXISTS 子查询的详细信息,请参阅 这篇文章.

For details of how SQL Server processes EXISTS Subqueries in CASE Expressions see this article.