以下查询将创建标记输出:
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
将呈现为:
更新
要匹配问题更新:
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
0
dept_code
更新 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() ,
) ,
)
演示
现在您可以编写一些递归函数来遍历树:
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>
演示
渲染: