在MySQL中使用SQL创建目录树

时间:2015-01-06 04:33:53

标签: mysql sql

我使用php制作目录树列表。目录(数据)来自MySQL数据库表,但我不想写sql来获取我想要的顺序的记录:

创建表的sql:

CREATE TABLE section (
  section_id tinyint(4) NOT NULL auto_increment,
  name varchar(500) default NULL,
  parent_id tinyint(4) default NULL,
  lineage varchar(45) default NULL,
  level tinyint(4) default NULL,
PRIMARY KEY  (section_id)
) ENGINE=MyISAM AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;

插入数据的SQL:

INSERT INTO section (
section_id,name,lineage,parent_id,level)
VALUES 
('1', 'Dashboard', '1', '0', '1'), 
('2', 'Slider', '2', '0', '1'), 
('3', 'Column', '3', '0', '1'),
('4', 'Column list', '3-4', '3', '2'), 
('5', 'Add column', '3-5', '3', '2'), 
('6', 'Permission', '6', '0', '1'), 
('7', 'Permission Group', '6-7', '6', '2'), 
('8', 'User List', '6-8', '6', '2'), 
('9', 'Section permission', '6-9', '6', '2'), 
('10', 'Add permission', '6-7-10', '7', '3'), 
('11', 'Add user', '6-8-11', '8', '3'), 
('12', 'Add section', '6-9-12', '9', '3');

表格的所有记录:

section_id     name                parent_id  level
-----------------------------------------------------
1              Dashboard           0          1
2              Slider              0          1
3              Column              0          1
4              Column list         3          2
5              Add column          3          2
6              Permission          0          1
7              Permission Group    6          2
8              User List           6          2
9              Section permission  6          2
10             Add permission      7          3 
11             Add user            8          3 
12             Add section         9          3

我想使用/ some SQL按此顺序获取行:

section_id     name                parent_id  level
-----------------------------------------------------
1              Dashboard           0          1
2              Slider              0          1
3              Column              0          1
4              Column list         3          2
5              Add column          3          2
6              Permission          0          1
7              Permission Group    6          2
10             Add permission      7          3 
8              User List           6          2
11             Add user            8          3 
9              Section permission  6          2
12             Add section         9          3

制作此目录树:

- Dashboard
- Slider
- Column
  - Column list
  - Add column
- Permission
  - Permission Group
    - Add permission
  - User List
    - Add user
  - Section permission
    - Add Section

我有一个想法,使用许多SQL来获取目录并将数据存储到php对象中。 这是获取子目录的SQL:

SELECT * FROM tbl_section AS a1 WHERE a1.parent_id = 0;
SELECT * FROM tbl_section AS a1 WHERE a1.parent_id = 1;
SELECT * FROM tbl_section AS a1 WHERE a1.parent_id = 2;
SELECT * FROM tbl_section AS a1 WHERE a1.parent_id = 3;

等等。

但是,如果有100个目录,它需要运行select sql 100次, 我认为这不是一个好方法,其他人都不知道吗?

1 个答案:

答案 0 :(得分:1)

这个方法(Lineage Column)可能不是列出从MySQL表中获取数据的目录树的最佳和最动态的方法,但这非常容易和快速:

stackoverflow中的相关问题:
What are the options for storing hierarchical data in a relational database?
参考文章: 谱系列(a.k.a.物化路径,路径枚举) http://www.ferdychristant.com/blog/articles/DOMM-7QJPM7


这是使用" Lineage Column"选择SQL。方法:

SELECT c.section_id, c.name, c.lineage, c.level, c.parent_id, 
    (SELECT COUNT(*) FROM section 
     where section.lineage 
     LIKE (CONCAT(c.lineage,'%')) AND 
     section.lineage!=c.lineage) as replies
FROM section as c
order by c.lineage;

它返回包含我想要的记录的记录:

section_id  name                lineage     level  parent_id   replies
----------------------------------------------------------------------
1           Dashboard           1           1      0           0
2           Slider              2           1      0           0
3           Column              3           1      0           2
4           Column list         3-4         2      3           0
5           Add column          3-5         2      3           0
6           Permission          6           1      0           6
7           Permission Group    6-7         2      6           1
10          Add permission      6-7-10      3      7           0
8           User List           6-8         2      6           1
11          Add user            6-8-11      3      8           0
9           Section permission  6-9         2      6           1
12          Add section         6-9-12      3      9           0

这是回复问题中提到的目录的PHP代码:

<?php
CONST SERVERNAME = "";  //your mysql server name
CONST USERNAME   = "";  //your mysql user name
CONST PASSWORD   = "";  //your mysql password
CONST DATABASE   = "";  //your mysql database name

// Create connection
$conn = new mysqli(SERVERNAME, USERNAME, PASSWORD, DATABASE);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

   $sql = "SELECT c.section_id, c.name, c.lineage, c.level, c.parent_id, 
        (SELECT COUNT(*) FROM section 
         where section.lineage 
         LIKE (CONCAT(c.lineage,'%')) AND 
         section.lineage!=c.lineage) as replies
    FROM section as c
    order by c.lineage";

    $html = "";
    foreach ($conn->query($sql) as $row) {
        switch ($row['level']){
            case "1": $html.="-".$row['name'] . "<br />"; break;
            case "2": $html.="&nbsp;&nbsp;-".$row['name'] . "<br />"; break;
            case "3": $html.="&nbsp;&nbsp;&nbsp;&nbsp;-".$row['name'] . "<br />"; break;
        } 
    }
    print $html;
?>

php代码的结果:

-Dashboard
-Slider
-Column
  -Column list
  -Add column
-Permission
  -Permission Group
    -Add permission
  -User List
    -Add user
  -Section permission
    -Add section