如何使用自加入 SQL 服务器在单个 table 上映射 child parent?

How to map child parent on a single table using self Join in SQL Server?

我有以下 table 与 child parent 关系。

ID      Title               PageID  IsParent    ParentID    IsActive
1       Dashboard           2125    True        NULL        True
2       Site Analytics      22      False       NULL        True
3       SEO Management      1       NULL        NULL        True
4       Mail Management     32      NULL        NULL        True
5       Build Mobile App    3214    NULL        NULL        True
6       Market Analytics    1321    NULL        NULL        True
7       Customize           235345  NULL        NULL        True
8       Reporter            253     NULL        NULL        True
9       Editor              545     NULL        NULL        True
10      News Template       45      NULL        NULL        True
11      Test Menu           0       True        3           True
NULL    NULL                NULL    NULL        NULL        NULL

这里ParentID定义了parent和child之间的关系。例如上面的table测试菜单就是Site Analytics的child。我有以下 SQL 查询。

SELECT
        P.ID
        ,P.Title AS Parent
        ,C.Title AS Child
        ,P.PageID
        ,P.IsParent
        ,P.ParentID
        ,P.IsActive
      FROM [dbo].[ChildParent] P
      LEFT JOIN [dbo].[ChildParent] C ON P.ID = C.ParentID

输出结果如下。

1   Dashboard           NULL            2125    1       NULL    1
2   Site Analytics      NULL            22      0       NULL    1
3   SEO Management      Test Menu       1       NULL    NULL    1
4   Mail Management     NULL            32      NULL    NULL    1
5   Build Mobile App    NULL            3214    NULL    NULL    1
6   Market Analytics    NULL            1321    NULL    NULL    1
7   Customize           NULL            235345  NULL    NULL    1
8   Reporter            NULL            253     NULL    NULL    1
9   Editor              NULL            545     NULL    NULL    1
10  News Template       NULL            45      NULL    NULL    1
11  Test Menu           NULL            0       1       3       1

基本上,我想要实现的是:

1   Dashboard           NULL            2125    1       NULL    1
2   Site Analytics      NULL            22      0       NULL    1
3   SEO Management      NULL            1       NULL    NULL    1
4   Mail Management     NULL            32      NULL    NULL    1
5   Build Mobile App    NULL            3214    NULL    NULL    1
6   Market Analytics    NULL            1321    NULL    NULL    1
7   Customize           NULL            235345  NULL    NULL    1
8   Reporter            NULL            253     NULL    NULL    1
9   Editor              NULL            545     NULL    NULL    1
10  News Template       NULL            45      NULL    NULL    1
11  Test Menu           SEO Management  0       1       3       1

在您的查询中试试这个小改动:

SELECT
        P.ID
        ,P.Title AS Parent
        ,C.Title AS Child
        ,P.PageID
        ,P.IsParent
        ,P.ParentID
        ,P.IsActive
      FROM [dbo].[ChildParent] P
      LEFT JOIN [dbo].[ChildParent] C ON isnull(P.ParentID, P.ID)  = c.id and C.ParentID is not null

你在倒退。

  FROM [dbo].[ChildParent] P
  LEFT JOIN [dbo].[ChildParent] C ON P.ParentID = C.ID