以下查询将创建标记输出:
SELECT group_concat(
CONCAT(
REPEAT(' ', (CHAR_LENGTH(t.dept_code) - 1) / 2),
'- ',
t.dept_name
)
ORDER BY t.dept_code
SEPARATOR '\n'
) AS markup
FROM Table1 t
sqlfiddle
结果:
- wadir Umum
- bagian umum
- kepala umum
- SUb bagian Tu
- bagian umum
- bagian privasi
将呈现给:
- 瓦迪尔乌姆
- bagian umum
- bagian privasi
更新
为了匹配问题更新:
SELECT t.*,
CHAR_LENGTH(t.dept_code) - CHAR_LENGTH(REPLACE(t.dept_code, '0', '')) AS indent,
CONCAT(
REPEAT('-', CHAR_LENGTH(t.dept_code) - CHAR_LENGTH(REPLACE(t.dept_code, '0', ''))),
t.dept_name
) AS indented_name
FROM Table1 t
ORDER BY t.dept_code
sqlfiddle
更新 2
您的设计的好处是您不需要存储过程来执行此类任务。查看dept_code 作为完整的树路径,0 作为分隔符。 1010102 也可以写成1/1/1/2。如果设计正确,您可以通过dept_code 订购。要获得节点深度,您只需计算路径 (dept_code) 中的分隔符 (0)。
更新 3
如果你想创建一个递归结构,你最好使用像 PHP 这样的过程语言:
将来自简单查询 (SELECT * FROM depts) 的 SQL 结果存储到数组中,如下所示:
// result from query: SELECT * FROM depts
$depts = array(
array( // row #0
'deptid' => 1,
'dept_code' => '1',
'dept_name' => 'wadir Umum',
'parent_deptid' => 0,
),
array( // row #1
'deptid' => 2,
'dept_code' => '101',
'dept_name' => 'bagian umum',
'parent_deptid' => 1,
),
array( // row #2
'deptid' => 3,
'dept_code' => '10101',
'dept_name' => 'kepala umum',
'parent_deptid' => 2,
),
array( // row #3
'deptid' => 4,
'dept_code' => '102',
'dept_name' => 'bagian privasi',
'parent_deptid' => 1,
),
array( // row #4
'deptid' => 5,
'dept_code' => '1010101',
'dept_name' => 'SUb bagian Tu',
'parent_deptid' => 3,
),
array( // row #5
'deptid' => 6,
'dept_code' => '1010102',
'dept_name' => 'bagian umum',
'parent_deptid' => 3,
),
);
使用两个foreach 循环构建递归结构:
$nodes = array();
$roots = array();
// init nodes
foreach ($depts as $dept) {
$dept['childs'] = array(); // init childs
$nodes[$dept['deptid']] = $dept;
}
foreach ($depts as $dept) {
if ($dept['parent_deptid'] == 0) {
$roots[] = $dept['deptid']; // add root
} else {
$nodes[$dept['parent_deptid']]['childs'][] = $dept['deptid']; // add to parents chlids list
}
}
数组$roots 和$nodes 如下所示:
$roots = array (0 => 1,);
$nodes = array(
1 => array(
'deptid' => 1,
'dept_code' => '1',
'dept_name' => 'wadir Umum',
'parent_deptid' => 0,
'childs' => array(
0 => 2,
1 => 4,
) ,
) ,
2 => array(
'deptid' => 2,
'dept_code' => '101',
'dept_name' => 'bagian umum',
'parent_deptid' => 1,
'childs' => array(
0 => 3,
) ,
) ,
3 => array(
'deptid' => 3,
'dept_code' => '10101',
'dept_name' => 'kepala umum',
'parent_deptid' => 2,
'childs' => array(
0 => 5,
1 => 6,
) ,
) ,
4 => array(
'deptid' => 4,
'dept_code' => '102',
'dept_name' => 'bagian privasi',
'parent_deptid' => 1,
'childs' => array() ,
) ,
5 => array(
'deptid' => 5,
'dept_code' => '1010101',
'dept_name' => 'SUb bagian Tu',
'parent_deptid' => 3,
'childs' => array() ,
) ,
6 => array(
'deptid' => 6,
'dept_code' => '1010102',
'dept_name' => 'bagian umum',
'parent_deptid' => 3,
'childs' => array() ,
) ,
)
Demo
现在您可以编写一些递归函数来遍历树:
function getSubtreeHTMLList($deptsids, $nodes) {
$result = '<ul>';
foreach ($deptsids as $deptsid) {
$result .= '<li>';
$result .= $nodes[$deptsid]['dept_name'];
if (count($nodes[$deptsid]['childs'] > 0)) {
$result .= getSubtreeHTMLList($nodes[$deptsid]['childs'], $nodes);
}
$result .= '</li>';
}
$result .= '</ul>';
return $result;
}
echo getSubtreeHTMLList($roots, $nodes);
创建的 HTML:
<ul><li>wadir Umum<ul><li>bagian umum<ul><li>kepala umum<ul><li>SUb bagian Tu<ul></ul></li><li>bagian umum<ul></ul></li></ul></li></ul></li><li>bagian privasi<ul></ul></li></ul></li></ul>
Demo
渲染:
- 瓦迪尔乌姆姆
- bagian umum
- bagian privasi