3

下面是我桌子上的东西。

我的表

++++++++++++++++++++
Parent   +  Child
++++++++++++++++++++
  C1     +    G1
  C1     +    G2
  C1     +    G3
  G3     +    D1
  G3     +    D2
  C1     +    G4
  G4     +    D3
  G4     +    D4
  C2     +    G5
  C2     +    G6
  C2     +    G7
  C2     +    G8
 ++++++++++++++++++++

什么,我想要如下使用MYSQL。

C1
  G1
  G2
  G3
    D1
    D2
  G4
    D3
    D4

C2
  G5
  G6
  G7
  G8

请让我知道这在 MYSQL 中是否可行。输出类似于TREE

更新 1

如果我得到像下面这样的新表也很好,这样我就可以使用这个例子

++++++++++++++++++++++++++++++++++++++++
Parent   +  Child   + PLevel  + CLevel
++++++++++++++++++++++++++++++++++++++++
  C1     +    G1    +   1    +   2
  C1     +    G2    +   1    +   2
  C1     +    G3    +   1    +   2
  G3     +    D1    +   2    +   3
  G3     +    D2    +   2    +   3
  C1     +    G4    +   1    +   2
  G4     +    D3    +   2    +   3
  G4     +    D4    +   2    +   3
  C2     +    G5    +   1    +   2
  C2     +    G6    +   1    +   2
  C2     +    G7    +   1    +   2
  C2     +    G8    +   1    +   2
++++++++++++++++++++++++++++++++++++++++

注意:我从 1 开始级别(例如,我从 0 开始级别)。如果我得到这个级别从 0 开始的新表也可以。

4

4 回答 4

0

首先为计算级别创建递归函数。

function fn_CalcLevel(int @ID) 
As Begin
  Declare @ParentID int
  Select @ParentID = ParentID From Table1 where ID = @ID

  IF (@ParentID IS NULL) Return 1 Else Return 1+fn_CalcLevel(@ParentID)
End

然后创建您的查询,如下所示

Select *, fn_CalcLevel(Table1.ID) as Level
From Table1
于 2012-05-28T04:30:18.933 回答
0

MySQL 和 RDBMS 通常不擅长这种结构。您可能必须使用客户端递归来执行此操作。

如果递归仅限于三个深度,就像您的示例一样,您可以使用连接来完成,但对于更深的树来说它不是很可扩展。

于 2012-05-21T16:32:31.587 回答
0

尽管您不能使用单个查询,但可以使用存储过程...唯一的前提条件是,您需要在现有示例表中再添加 2 条记录以表示“C1”和“C2”是顶层...添加一条记录,其中“父”字段为空白,子级别为“C1”,另一个为“C2”。这将“准备”最顶层的父级。对于后续的层次关联,否则你没有顶层层次的起始“基础”。它还需要一个“主键”列(我在此脚本中将其创建为“IDMyTable”,它只是 1-x 连续的,但假设您的表上有一个自动增量列可供使用)。

我已经包含了所有输出列以显示它是如何构建的,但此例程的前提是根据预期的列输出创建一个表,但在构建时还额外保存下游的分层表示。为了确保它们在层变深时保持正确的方向,我连接了“ID”列——你会看到它在最终结果集中是如何工作的。

然后,在最终结果集中,我根据层次结构数据的深度预先填充空间。

循环将根据在前面结果集中找到的父记录添加任何记录,但前提是尚未添加 ID(防止重复)...

要查看循环顺序是如何不断附加的,您可以运行最后一个查询而不使用 order by,并查看每次迭代如何限定并添加上一个层次结构级别...

-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `GetHierarchy2`()
BEGIN
    -- prepare a hierarchy level variable 
    set @hierlvl := 00000;

    -- prepare a variable for total rows so we know when no more rows found
    set @lastRowCount := 0;

    -- pre-drop temp table
    drop table if exists MyHierarchy;

    -- now, create it as the first level you want... 
    -- ie: a specific top level of all "no parent" entries
    -- or parameterize the function and ask for a specific "ID".
    -- add extra column as flag for next set of ID's to load into this.
    create table MyHierarchy as
    select 
            t1.IDMyTable,
            t1.Child AS Parent,
            @hierlvl as IDHierLevel,
            cast( t1.IDMyTable as char(100)) FullHierarchy
        from
            MyTable t1
        where
                t1.Parent is null
            OR t1.Parent = '';


    -- how many rows are we starting with at this tier level
    set @lastRowCount := ROW_COUNT();

    -- we need to have a "primary key", otherwise our UPDATE
    -- statement will nag about an unsafe update command
    alter table MyHierarchy add primary key (IDMyTable);


    -- NOW, keep cycling through until we get no more records
    while @lastRowCount > 0 do

        -- NOW, load in all entries found from full-set NOT already processed
        insert into MyHierarchy
            select 
                    t1.IDMyTable,
                    t1.Child as Parent,
                    h1.IDHierLevel +1 as IDHierLevel,
                    concat_ws( ',', h1.FullHierarchy, t1.IDMyTable ) as FullHierarchy
                from
                    MyTable t1
                        join MyHierarchy h1
                            on t1.Parent = h1.Parent
                    left join
                        MyHierarchy h2
                            on t1.IDMyTable = h2.IDMyTable
                where
                    h2.IDMyTable is null;


        set @lastRowCount := row_count();

        -- now, update the hierarchy level
        set @hierLevel := @hierLevel +1;

    end while;


    -- return the final set now
    select 
            *, concat( lpad( ' ', 1 + (IDHierLevel * 3 ), ' ' ), Parent ) as ShowHierarchy
        from MyHierarchy
        order by FullHierarchy;

END
于 2012-05-27T13:50:52.390 回答
0

如果你稍微调整一下你的表,你可以使用类似的东西:

  SELECT Child,CONCAT(LPAD('',Clevel,' '),Child),etc from tablename

重组是您需要将根节点作为父节点为 0 的行。您可以添加自己的父/子/C 级别的排序以获得所需的序列。

我知道这是几年前的事了,但它可能会为其他人节省一些精力!

于 2015-11-13T15:17:50.757 回答