数据库
首页 > 数据库> > 树形式的MYSQL输出或添加级别(Parent-Child)

树形式的MYSQL输出或添加级别(Parent-Child)

作者:互联网

以下是我在桌子上的内容.

为myTable

++++++++++++++++++++
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

如果我得到像下面这样的新表也没关系,所以我可以使用this example.

++++++++++++++++++++++++++++++++++++++++
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开始的新表也没关系.

解决方法:

虽然您不能使用单个查询,但您可以使用存储过程…唯一的预先要求,您需要在现有样本表中添加2个以上的记录来表示“C1”和“C2”ARE顶级…添加“父”字段为空的记录,子级别为“C1”,另一个为“C2”.这将“准备”最顶层的父级.对于后续层次结构关联,否则您没有顶层层次结构的起始“基础”.它还需要一个“主键”列(我在这个脚本中创建的“IDMyTable”只是1-x顺序,但假设你的表上有一个自动增量列来代替).

我已经包含了所有输出列以显示它是如何构建的,但是这个例程的前提是基于预期的列输出创建一个表,还有额外的用于在构建时保持下游的层次表示.为了确保它们保持正确的方向,因为层越来越深,我正在整理“ID”列 – 你会看到它在最终结果集中是如何工作的.

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

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

要查看循环顺序是如何不断附加的,您可以运行最后一个查询而不用顺序,并查看每个迭代如何合格并添加先前的层次结构级别…

-- --------------------------------------------------------------------------------
-- 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

标签:sql,mysql,format,tree,parent-child
来源: https://codeday.me/bug/20191002/1841276.html