嗨,我正在使用codeigniter和MySQL,我有这样的表结构。

当我传递父标题时,我将得到所有的子值。
这是密码
<?php <br>
// $parent is the parent of the children we want to see <br>
// $level is increased when we go deeper into the tree, <br>
// used to display a nice indented tree <br>
function display_children($parent, $level) { <br>
// retrieve all children of $parent <br>
$result = mysql_query('SELECT title FROM tree '. <br>
'WHERE parent="'.$parent.'";'); <br>
<br>
// display each child <br>
while ($row = mysql_fetch_array($result)) { <br>
// indent and display the title of this child <br>
echo str_repeat(' ',$level).$row['title']."\n"; <br>
<br>
// call this function again to display this <br>
// child's children <br>
display_children($row['title'], $level+1); <br>
} <br>
} <br>
?>当我通过display_children('',0);的时候
Food<br>
Fruit<br>
Red<br>
Cherry<br>
Yellow<br>
Banana<br>
Meat<br>
Beef<br>
Pork要显示“果树”子树,您可以运行display_children('Fruit',0);
因此,我计划编写一个存储过程,因为它是有效的。有可能吗??如有可能,请给我看一个示例代码,谢谢.
发布于 2012-07-23 07:24:12
MySQL存储过程有更多的缺点而不是优点。Imho,您应该非常小心地考虑使用这个特性。
主要的缺点之一是它不能被版本化。你只有一个版本,仅此而已。
无论是mysql、oracle、postgre等,在存储引擎中包含业务逻辑都是有害的。
考虑到这些缺点,您还应该考虑这样一个事实,即您没有使用存储过程的额外好处--没有性能增益。纳达!
如果是在您的情况下,我要做的就是将数据提取出来,并在php中完成递归处理。
https://stackoverflow.com/questions/11601976
复制相似问题