递归查询SQL Server


Recursive Query SQL Server

我被这个问题困了一段时间了

我有一个名为Category的表它是这样的

number, name, parent
1     , Fruit, null
2     , Apples, 1
3     , Golden Apples, 2
4     , Pineapples, 1

现在,我应该如何选择主类别和它的子类别,如果它们存在,它们的子类别。

我必须在PHP中像这样输出
<div>Fruit
     <div>Apples
          <div>Golden Apples</div>
     </div>
     <div>Pineapples</div>
</div>

理论上,你可以拥有的子类别没有已知的数量或数量。这个问题我已经纠结了一段时间了。

可以通过循环执行此操作,然后再次运行查询以选择其子类别,但是这将在数据库端占用非常多的资源。

我希望有人能帮我解决这个问题。

理想的方法是对数据进行规范化。创建一个类别表和一个保存每个类别成员的表。然后,您可以在表之间执行一个简单的左连接,以获得您想要的结果。由于您没有这样做,因此您需要从主表中进行选择,然后使用别名在同一表上执行左连接。

这样就可以了。

从父级为NULL的类别中选择号码,名称左加入类别为Items的项目。父=类别。数

尝试像这样使用递归CTE:

--this is just an old example I've used before, but it kind of matches your table
--go through a nested table supervisor - user table and display the chain
DECLARE @Contacts table (id int, first_name varchar(10), reports_to_id int)
INSERT @Contacts VALUES (1,'Jerome', NULL )  -- tree is as follows:
INSERT @Contacts VALUES (2,'Joe'   ,'1')     --                      1-Jerome
INSERT @Contacts VALUES (3,'Paul'  ,'2')     --                     /        '
INSERT @Contacts VALUES (4,'Jack'  ,'3')     --              2-Joe           9-Bill
INSERT @Contacts VALUES (5,'Daniel','3')     --            /       '              '
INSERT @Contacts VALUES (6,'David' ,'2')     --     3-Paul          6-David       10-Sam
INSERT @Contacts VALUES (7,'Ian'   ,'6')     --    /      '            /    '
INSERT @Contacts VALUES (8,'Helen' ,'6')     -- 4-Jack  5-Daniel   7-Ian    8-Helen
INSERT @Contacts VALUES (9,'Bill ' ,'1')     --
INSERT @Contacts VALUES (10,'Sam'  ,'9')     --
DECLARE @Root_id  int
--get complete tree---------------------------------------------------
SET @Root_id=null
PRINT '@Root_id='+COALESCE(''''+CONVERT(varchar(5),@Root_id)+'''','null')
;WITH StaffTree AS
(
    SELECT 
        c.id, c.first_name, c.reports_to_id, c.reports_to_id as Manager_id, cc.first_name AS Manager_first_name, 1 AS LevelOf
        FROM @Contacts                  c
            LEFT OUTER JOIN @Contacts  cc ON c.reports_to_id=cc.id
        WHERE c.id=@Root_id OR (@Root_id IS NULL AND c.reports_to_id IS NULL)
    UNION ALL
        SELECT 
            s.id, s.first_name, s.reports_to_id, t.id, t.first_name, t.LevelOf+1
        FROM StaffTree            t
            INNER JOIN @Contacts  s ON t.id=s.reports_to_id
    WHERE s.reports_to_id=@Root_id OR @Root_id IS NULL OR t.LevelOf>1
)
SELECT * FROM StaffTree ORDER BY LevelOf,first_name
输出:

@Root_id=null
         id first_name reports_to_id  Manager_id Manager_first_name     LevelOf
----------- ---------- ------------- ----------- ------------------ -----------
          1 Jerome              NULL        NULL NULL                         1
          9 Bill                   1           1 Jerome                       2
          2 Joe                    1           1 Jerome                       2
          6 David                  2           2 Joe                          3
          3 Paul                   2           2 Joe                          3
         10 Sam                    9           9 Bill                         3
          5 Daniel                 3           3 Paul                         4
          8 Helen                  6           6 David                        4
          7 Ian                    6           6 David                        4
          4 Jack                   3           3 Paul                         4
(10 row(s) affected)

我认为你可以在PHP中循环结果集并构建你的div。