从同一个表中选择父级和子级

发布于 2024-12-15 06:13:12 字数 755 浏览 0 评论 0原文

我有一个 emp 表,

    CREATE TABLE [dbo].[Emp](
    [EmpId] [int] NULL,
    [EmpName] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [ManagerId] [int] NULL
) ON [PRIMARY]

现在,将以下值插入到表中,

   Insert Into Emp Values(1,'A',0)
Insert Into Emp Values(2,'B',1)
Insert Into Emp Values(3,'C',2)
Insert Into Emp Values(4,'D',2)
Insert Into Emp Values(5,'E',4)
Insert Into Emp Values(6,'F',4)
Insert Into Emp Values(7,'G',4)
Insert Into Emp Values(8,'H',6)
Insert Into Emp Values(9,'I',5)
Insert Into Emp Values(10,'J',7)
Insert Into Emp Values(11,'K',4)

我想在 select 语句中列出员工姓名及其经理姓名。

我现在正在做的是创建一个临时表,其中包含所有经理的姓名及其 ID。

然后根据Id从manager表中获取名字。

但我知道这不是正确的方法,事实上它很复杂。

I have a emp table,

    CREATE TABLE [dbo].[Emp](
    [EmpId] [int] NULL,
    [EmpName] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [ManagerId] [int] NULL
) ON [PRIMARY]

Now, insert below values into the table

   Insert Into Emp Values(1,'A',0)
Insert Into Emp Values(2,'B',1)
Insert Into Emp Values(3,'C',2)
Insert Into Emp Values(4,'D',2)
Insert Into Emp Values(5,'E',4)
Insert Into Emp Values(6,'F',4)
Insert Into Emp Values(7,'G',4)
Insert Into Emp Values(8,'H',6)
Insert Into Emp Values(9,'I',5)
Insert Into Emp Values(10,'J',7)
Insert Into Emp Values(11,'K',4)

I want to list employee name and their manager name in select statement.

What I am doing now is creating a temporary table which has all manager name and their Id.

Then getting the name from the manager table based on Id.

But I know this is not a correct way, in fact it is complex.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灵芸 2024-12-22 06:13:12

为此,您应该使用递归 CTE(通用表表达式):

-- define the recursive CTE and give it a name
;WITH Hierarchy AS
(
    -- "anchor" - top-level rows to select, here those with ManagerId = 0
    SELECT EmpId, EmpName, NULL AS 'MgrId', CAST(NULL AS NVARCHAR(50)) AS 'MgrName', 1 AS 'Level'
    FROM dbo.Emp
    WHERE ManagerId = 0

    UNION ALL

    -- recursive part - join an employee to its manager via ManagerId -> mgr.EmpId
    SELECT e.EmpId, e.EmpName, mgr.EmpId, mgr.EmpName, mgr.Level + 1 AS 'Level'
    FROM dbo.Emp e
    INNER JOIN Hierarchy mgr ON e.ManagerId = mgr.EmpId
)
SELECT * FROM Hierarchy

You should use a recursive CTE (Common Table Expression) for this:

-- define the recursive CTE and give it a name
;WITH Hierarchy AS
(
    -- "anchor" - top-level rows to select, here those with ManagerId = 0
    SELECT EmpId, EmpName, NULL AS 'MgrId', CAST(NULL AS NVARCHAR(50)) AS 'MgrName', 1 AS 'Level'
    FROM dbo.Emp
    WHERE ManagerId = 0

    UNION ALL

    -- recursive part - join an employee to its manager via ManagerId -> mgr.EmpId
    SELECT e.EmpId, e.EmpName, mgr.EmpId, mgr.EmpName, mgr.Level + 1 AS 'Level'
    FROM dbo.Emp e
    INNER JOIN Hierarchy mgr ON e.ManagerId = mgr.EmpId
)
SELECT * FROM Hierarchy
穿越时光隧道 2024-12-22 06:13:12

你是对的:你不必为此使用临时表。尝试使用递归查询。 查看 MSDN 上的此链接。有一个 ManagerId/EmployeeID 的示例。就像你的查询一样。

You are correct: you don't have to use a temporary table just for this. Try using recursive queries. Take a look at this link on MSDN. There is an example with ManagerId/EmployeeID. Just as in your query.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文