将具有级别的树节点转换为行列表

时间:2012-03-27 20:46:43

标签: mysql

我在这里有一点挑战,有数据:

CREATE TABLE vCategoryTree (
    level1 INT UNSIGNED DEFAULT NULL,
    level2 INT UNSIGNED DEFAULT NULL,
    level3 INT UNSIGNED DEFAULT NULL,
    level4 INT UNSIGNED DEFAULT NULL,
    level5 INT UNSIGNED DEFAULT NULL
);

INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 43, 44, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 43, 45, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 43, 46, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 47, 48, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 47, 49, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 47, 50, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 47, 51, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 47, 52, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 47, 53, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 47, 54, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 47, 55, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 47, 56, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 47, 57, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 47, 58, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 47, 59, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 47, 60, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 61, 62, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 61, 63, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 61, 64, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 61, 65, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 61, 66, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 61, 67, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 61, 68, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 61, 69, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 61, 70, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 4, 9, 10, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 4, 9, 25, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 4, 9, 26, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 4, 11, 12, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 4, 11, 13, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 4, 14, 15, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 4, 14, 16, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 4, 14, 17, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 4, 14, 18, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 4, 14, 19, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 4, 14, 20, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 4, 14, 21, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 4, 14, 22, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 4, 14, 23, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 4, 14, 24, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 4, 14, 27, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 5, 28, 29, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 5, 28, 34, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 5, 28, 37, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 5, 28, 38, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 5, 28, 39, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 5, 28, 41, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 5, 28, 42, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 5, 30, 31, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 5, 30, 35, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 5, 30, 40, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 5, 32, 33, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 5, 32, 36, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 6, 7, NULL, NULL);
INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 6, 8, NULL, NULL);

这是在MySQL中将邻接模型转换为修改后的预订树taversal模型的各种实验的结果。

我需要做的是从这些级别生成一个列表到基于行的结果树,例如,我希望得到:

2
43
45
47
48
49
50
51
...
4
9
10
25
26
11
12
13
...
32
33
36
6
7
8

......只是预期数据中的一个切口,所以我可以提供更多的示例而不会实际粘贴我需要的所有内容,我不期望......出现在那里。

希望这已经足够了......

不,我不想通过PHP ...

谢谢

2 个答案:

答案 0 :(得分:0)

首先,让我们加载您的样本数据

mysql> drop database if exists mathieu;
Query OK, 2 rows affected (0.11 sec)

mysql> create database mathieu;
Query OK, 1 row affected (0.00 sec)

mysql> use mathieu
Database changed
mysql> CREATE TABLE vCategoryTree (
    ->     level1 INT UNSIGNED DEFAULT NULL,
    ->     level2 INT UNSIGNED DEFAULT NULL,
    ->     level3 INT UNSIGNED DEFAULT NULL,
    ->     level4 INT UNSIGNED DEFAULT NULL,
    ->     level5 INT UNSIGNED DEFAULT NULL
    -> );
Query OK, 0 rows affected (0.08 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 43, 44, NULL);
Query OK, 1 row affected (0.06 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 43, 45, NULL);
Query OK, 1 row affected (0.06 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 43, 46, NULL);
Query OK, 1 row affected (0.06 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 47, 48, NULL);
Query OK, 1 row affected (0.05 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 47, 49, NULL);
Query OK, 1 row affected (0.07 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 47, 50, NULL);
Query OK, 1 row affected (0.06 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 47, 51, NULL);
Query OK, 1 row affected (0.06 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 47, 52, NULL);
Query OK, 1 row affected (0.06 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 47, 53, NULL);
Query OK, 1 row affected (0.05 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 47, 54, NULL);
Query OK, 1 row affected (0.07 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 47, 55, NULL);
Query OK, 1 row affected (0.06 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 47, 56, NULL);
Query OK, 1 row affected (0.05 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 47, 57, NULL);
Query OK, 1 row affected (0.06 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 47, 58, NULL);
Query OK, 1 row affected (0.06 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 47, 59, NULL);
Query OK, 1 row affected (0.10 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 47, 60, NULL);
Query OK, 1 row affected (0.07 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 61, 62, NULL);
Query OK, 1 row affected (0.06 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 61, 63, NULL);
Query OK, 1 row affected (0.07 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 61, 64, NULL);
Query OK, 1 row affected (0.06 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 61, 65, NULL);
Query OK, 1 row affected (0.05 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 61, 66, NULL);
Query OK, 1 row affected (0.07 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 61, 67, NULL);
Query OK, 1 row affected (0.06 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 61, 68, NULL);
Query OK, 1 row affected (0.05 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 61, 69, NULL);
Query OK, 1 row affected (0.07 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 2, 61, 70, NULL);
Query OK, 1 row affected (0.06 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 4, 9, 10, NULL);
Query OK, 1 row affected (0.06 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 4, 9, 25, NULL);
Query OK, 1 row affected (0.05 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 4, 9, 26, NULL);
Query OK, 1 row affected (0.06 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 4, 11, 12, NULL);
Query OK, 1 row affected (0.05 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 4, 11, 13, NULL);
Query OK, 1 row affected (0.06 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 4, 14, 15, NULL);
Query OK, 1 row affected (0.06 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 4, 14, 16, NULL);
Query OK, 1 row affected (0.05 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 4, 14, 17, NULL);
Query OK, 1 row affected (0.06 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 4, 14, 18, NULL);
Query OK, 1 row affected (0.06 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 4, 14, 19, NULL);
Query OK, 1 row affected (0.06 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 4, 14, 20, NULL);
Query OK, 1 row affected (0.07 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 4, 14, 21, NULL);
Query OK, 1 row affected (0.06 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 4, 14, 22, NULL);
Query OK, 1 row affected (0.06 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 4, 14, 23, NULL);
Query OK, 1 row affected (0.06 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 4, 14, 24, NULL);
Query OK, 1 row affected (0.05 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 4, 14, 27, NULL);
Query OK, 1 row affected (0.05 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 5, 28, 29, NULL);
Query OK, 1 row affected (0.06 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 5, 28, 34, NULL);
Query OK, 1 row affected (0.05 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 5, 28, 37, NULL);
Query OK, 1 row affected (0.07 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 5, 28, 38, NULL);
Query OK, 1 row affected (0.05 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 5, 28, 39, NULL);
Query OK, 1 row affected (0.06 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 5, 28, 41, NULL);
Query OK, 1 row affected (0.07 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 5, 28, 42, NULL);
Query OK, 1 row affected (0.05 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 5, 30, 31, NULL);
Query OK, 1 row affected (0.05 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 5, 30, 35, NULL);
Query OK, 1 row affected (0.07 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 5, 30, 40, NULL);
Query OK, 1 row affected (0.05 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 5, 32, 33, NULL);
Query OK, 1 row affected (0.07 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 5, 32, 36, NULL);
Query OK, 1 row affected (0.06 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 6, 7, NULL, NULL);
Query OK, 1 row affected (0.06 sec)

mysql> INSERT INTO `vCategoryTree` (`level1`, `level2`, `level3`, `level4`, `level5`) VALUES (0, 6, 8, NULL, NULL);
Query OK, 1 row affected (0.07 sec)

mysql> select * from vCategoryTree;
+--------+--------+--------+--------+--------+
| level1 | level2 | level3 | level4 | level5 |
+--------+--------+--------+--------+--------+
|      0 |      2 |     43 |     44 |   NULL |
|      0 |      2 |     43 |     45 |   NULL |
|      0 |      2 |     43 |     46 |   NULL |
|      0 |      2 |     47 |     48 |   NULL |
|      0 |      2 |     47 |     49 |   NULL |
|      0 |      2 |     47 |     50 |   NULL |
|      0 |      2 |     47 |     51 |   NULL |
|      0 |      2 |     47 |     52 |   NULL |
|      0 |      2 |     47 |     53 |   NULL |
|      0 |      2 |     47 |     54 |   NULL |
|      0 |      2 |     47 |     55 |   NULL |
|      0 |      2 |     47 |     56 |   NULL |
|      0 |      2 |     47 |     57 |   NULL |
|      0 |      2 |     47 |     58 |   NULL |
|      0 |      2 |     47 |     59 |   NULL |
|      0 |      2 |     47 |     60 |   NULL |
|      0 |      2 |     61 |     62 |   NULL |
|      0 |      2 |     61 |     63 |   NULL |
|      0 |      2 |     61 |     64 |   NULL |
|      0 |      2 |     61 |     65 |   NULL |
|      0 |      2 |     61 |     66 |   NULL |
|      0 |      2 |     61 |     67 |   NULL |
|      0 |      2 |     61 |     68 |   NULL |
|      0 |      2 |     61 |     69 |   NULL |
|      0 |      2 |     61 |     70 |   NULL |
|      0 |      4 |      9 |     10 |   NULL |
|      0 |      4 |      9 |     25 |   NULL |
|      0 |      4 |      9 |     26 |   NULL |
|      0 |      4 |     11 |     12 |   NULL |
|      0 |      4 |     11 |     13 |   NULL |
|      0 |      4 |     14 |     15 |   NULL |
|      0 |      4 |     14 |     16 |   NULL |
|      0 |      4 |     14 |     17 |   NULL |
|      0 |      4 |     14 |     18 |   NULL |
|      0 |      4 |     14 |     19 |   NULL |
|      0 |      4 |     14 |     20 |   NULL |
|      0 |      4 |     14 |     21 |   NULL |
|      0 |      4 |     14 |     22 |   NULL |
|      0 |      4 |     14 |     23 |   NULL |
|      0 |      4 |     14 |     24 |   NULL |
|      0 |      4 |     14 |     27 |   NULL |
|      0 |      5 |     28 |     29 |   NULL |
|      0 |      5 |     28 |     34 |   NULL |
|      0 |      5 |     28 |     37 |   NULL |
|      0 |      5 |     28 |     38 |   NULL |
|      0 |      5 |     28 |     39 |   NULL |
|      0 |      5 |     28 |     41 |   NULL |
|      0 |      5 |     28 |     42 |   NULL |
|      0 |      5 |     30 |     31 |   NULL |
|      0 |      5 |     30 |     35 |   NULL |
|      0 |      5 |     30 |     40 |   NULL |
|      0 |      5 |     32 |     33 |   NULL |
|      0 |      5 |     32 |     36 |   NULL |
|      0 |      6 |      7 |   NULL |   NULL |
|      0 |      6 |      8 |   NULL |   NULL |
+--------+--------+--------+--------+--------+
55 rows in set (0.00 sec)

mysql>

现在这里是你需要的非常非常丑陋的查询

SELECT level,parent,IFNULL(child,'Leaf') child FROM (
    SELECT DISTINCT level1 parent,level2 child,1 level
    FROM vCategoryTree WHERE level1 IS NOT NULL OR level2 IS NOT NULL UNION
    SELECT DISTINCT level2       ,level3      ,2
    FROM vCategoryTree WHERE level2 IS NOT NULL OR level3 IS NOT NULL UNION
    SELECT DISTINCT level3       ,level4      ,3
    FROM vCategoryTree WHERE level3 IS NOT NULL OR level4 IS NOT NULL UNION
    SELECT DISTINCT level4       ,level5      ,4
    FROM vCategoryTree WHERE level4 IS NOT NULL OR level5 IS NOT NULL
) A order by level,parent,IFNULL(child,999999999);

它将显示每个父母和孩子以及父母的水平。

其子项为NULL的每一行都是树中的叶子

现在这里有两个存储过程,我希望它能为您提供基本上正在寻找的树状显示

第一个存储过程创建一个名为DisplayTree的表,并放置您选择的父级

DELIMITER $$

DROP PROCEDURE IF EXISTS `DisplayTree` $$
CREATE DEFINER=`lwdba`@`127.0.0.1` PROCEDURE `DisplayTree`(given_parent INT UNSIGNED)
BEGIN

    DROP TABLE IF EXISTS DisplayTree;
    CREATE TABLE DisplayTree
    (
        id INT NOT NULL AUTO_INCREMENT,
        DIsplayText VARCHAR(1024),
        PRIMARY KEY (id)
    ) ENGINE=MyISAM;

    INSERT INTO DisplayTree (DisplayText) SELECT LPAD(given_parent,4,' ');
    CALL LoadDisplayTree(given_parent,1);

    SELECT DisplayText FROM DisplayTree;

END $$

DELIMITER ;

第二个存储过程是一个递归的,用于填充临时表DisplayTree

DELIMITER $$

DROP PROCEDURE IF EXISTS `LoadDisplayTree` $$
CREATE DEFINER=`lwdba`@`127.0.0.1` PROCEDURE `LoadDisplayTree`(given_parent INT UNSIGNED,given_level INT UNSIGNED)
TheStoredProcedure:BEGIN

    DECLARE children VARCHAR(255);
    DECLARE howmanykids,ndx,ps1,ps2,next_child INT;

    SET children = '';

    SELECT COUNT(1) INTO howmanykids FROM
    (
        SELECT level,parent,child FROM (
        SELECT DISTINCT level1 parent,level2 child,1 level
        FROM vCategoryTree WHERE level1 IS NOT NULL OR level2 IS NOT NULL UNION
        SELECT DISTINCT level2       ,level3      ,2
        FROM vCategoryTree WHERE level2 IS NOT NULL OR level3 IS NOT NULL UNION
        SELECT DISTINCT level3       ,level4      ,3
        FROM vCategoryTree WHERE level3 IS NOT NULL OR level4 IS NOT NULL UNION
        SELECT DISTINCT level4       ,level5      ,4
        FROM vCategoryTree WHERE level4 IS NOT NULL OR level5 IS NOT NULL
        ) AA order by parent,level,IFNULL(child,999999999)
    ) A WHERE parent = given_parent AND child IS NOT NULL;

    IF howmanykids = 0 THEN
        LEAVE TheStoredProcedure;
    END IF;

    SELECT CONCAT(GROUP_CONCAT(child),',') INTO children FROM
    (
        SELECT level,parent,child FROM (
        SELECT DISTINCT level1 parent,level2 child,1 level
        FROM vCategoryTree WHERE level1 IS NOT NULL OR level2 IS NOT NULL UNION
        SELECT DISTINCT level2       ,level3      ,2
        FROM vCategoryTree WHERE level2 IS NOT NULL OR level3 IS NOT NULL UNION
        SELECT DISTINCT level3       ,level4      ,3
        FROM vCategoryTree WHERE level3 IS NOT NULL OR level4 IS NOT NULL UNION
        SELECT DISTINCT level4       ,level5      ,4
        FROM vCategoryTree WHERE level4 IS NOT NULL OR level5 IS NOT NULL
        ) AA order by parent,level,IFNULL(child,999999999)
    ) A WHERE parent = given_parent AND child IS NOT NULL;

    SET ndx = 0;
    SET ps2 = 0;
    WHILE ndx < howmanykids DO
        SET ps1 = ps2 + 1;
        SET ps2 = ps2 + 1;
        WHILE SUBSTR(children,ps2,1) <> ',' DO
            SET ps2 = ps2 + 1;
        END WHILE;
        SET next_child = SUBSTR(children,ps1,ps2 - ps1);
        INSERT INTO DisplayTree (DisplayText)
        SELECT REPLACE(CONCAT(LPAD('X',(given_level * 8),'X'),LPAD(next_child,4,'X')),'X',' ');
        CALL LoadDisplayTree(next_child,given_level + 1);
        SET ndx = ndx + 1;
    END WHILE;

END $$

DELIMITER ;
Give it a Try !!!

让我们以parent_id 0开始显示:

mysql> call displaytree(0);
+------------------------------+
| DisplayText                  |
+------------------------------+
|    0                         |
|            2                 |
|                   43         |
|                           44 |
|                           45 |
|                           46 |
|                   47         |
|                           48 |
|                           49 |
|                           50 |
|                           51 |
|                           52 |
|                           53 |
|                           54 |
|                           55 |
|                           56 |
|                           57 |
|                           58 |
|                           59 |
|                           60 |
|                   61         |
|                           62 |
|                           63 |
|                           64 |
|                           65 |
|                           66 |
|                           67 |
|                           68 |
|                           69 |
|                           70 |
|            4                 |
|                    9         |
|                           10 |
|                           25 |
|                           26 |
|                   11         |
|                           12 |
|                           13 |
|                   14         |
|                           15 |
|                           16 |
|                           17 |
|                           18 |
|                           19 |
|                           20 |
|                           21 |
|                           22 |
|                           23 |
|                           24 |
|                           27 |
|            5                 |
|                   28         |
|                           29 |
|                           34 |
|                           37 |
|                           38 |
|                           39 |
|                           41 |
|                           42 |
|                   30         |
|                           31 |
|                           35 |
|                           40 |
|                   32         |
|                           33 |
|                           36 |
|            6                 |
|                    7         |
|                    8         |
+------------------------------+
69 rows in set (0.20 sec)

Query OK, 0 rows affected (0.25 sec)

mysql>

您不必以parent_id 0开头。让我们试试parent_id 4:

mysql> call displaytree(4);
+----------------------+
| DisplayText          |
+----------------------+
|    4                 |
|            9         |
|                   10 |
|                   25 |
|                   26 |
|           11         |
|                   12 |
|                   13 |
|           14         |
|                   15 |
|                   16 |
|                   17 |
|                   18 |
|                   19 |
|                   20 |
|                   21 |
|                   22 |
|                   23 |
|                   24 |
|                   27 |
+----------------------+
20 rows in set (0.11 sec)

Query OK, 0 rows affected (0.12 sec)

mysql>

CAVEAT

由于涉及递归,请运行此

mysql> set max_sp_recursion_depth = 64;
Query OK, 0 rows affected (0.00 sec)

mysql>

在调用DisplayTree之前

答案 1 :(得分:0)

好吧,让我的肚子里装满了披萨,花了一些时间来解决这个问题并且能够让它工作,除了我不能创造一个视图。但它非常快速有效:

CREATE VIEW vCategoryTreeExploded AS 
    SELECT level1, level2, level3, level4, level5 FROM vCategoryTree WHERE level1 IS NOT NULL
    UNION ALL
    SELECT level1, level2, level3, level4, level5 FROM vCategoryTree WHERE level1 IS NOT NULL
    UNION ALL
    SELECT level1, level2, level3, level4, level5 FROM vCategoryTree WHERE level1 IS NOT NULL
    UNION ALL
    SELECT level1, level2, level3, level4, level5 FROM vCategoryTree WHERE level1 IS NOT NULL
    UNION ALL
    SELECT level1, level2, level3, level4, level5 FROM vCategoryTree WHERE level1 IS NOT NULL
    ORDER BY `level1`,`level2`,`level3`,`level4`,`level5`

因此,在第一个视图中,我创建了我的另一个视图,我能够复制最多5个示例中的所有行(仅保留非空行)并重新排序之前的所有内容。这给了我一张表,我可以使用一些变量来处理:

SELECT categoryid
FROM (
    SELECT
        CASE 
            WHEN IFNULL(@level1, -1) <> level1 THEN @level1 := level1
            WHEN IFNULL(@level2, -1) <> level2 THEN @level2 := level2
            WHEN IFNULL(@level3, -1) <> level3 THEN @level3 := level3
            WHEN IFNULL(@level4, -1) <> level4 THEN @level4 := level4
            WHEN IFNULL(@level5, -1) <> level5 THEN @level5 := level5
        END AS categoryid
    FROM vCategoryTreeExploded, (SELECT @output := NULL, @level1 := NULL, @level2 := NULL, @level3 := NULL, @level4 := NULL, @level5 := NULL) AS initialization
) AS subdata
WHERE categoryid IS NOT NULL

可悲的是,由于用户变量或子选择,我无法将此输出转换为视图,但由于它将一次性用于生成NestedSet插入列表,所以我会没事的。< / p>

我希望有人可以改进并尽可能让它“可见”,但如果可能的话,我会感到非常惊讶:P

谢谢大家的暗示......