MySql无限分类数据结构--预排序遍历树算法

无限分类是我们开发中非常常见的应用,像论坛的的版块,CMS的类别,应用的地方特别多。
我们最常见最简单的方法就是在MySql里ID ,parentID,name。其优点是简单,结构简单;缺点是效率不高,因为每一次递归都要查询数据库,几百条数据时就不是很快了!

存储树是一种常见的问题,多种解决方案。主要有两种方法:邻接表的模型,并修改树前序遍历算法。 

我们将探讨这两种方法的节能等级的数据。我会使用树从一个虚构的网上食品商店作为一个例子。这食品商店组织其食品类,通过颜色和类型。这棵树看起来像这样: 


id="iframe_0.34437359676710066" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/images/blogjava_net/lijie250/sitepoint_tree.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.34437359676710066',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 231px; height: 259px;">


下面我们将用另外一种方法,这就是预排序遍历树算法(modified preorder tree traversal algorithm) 
这种方法大家可能接触的比较少,初次使用也不像上面的方法容易理解,但是由于这种方法不使用递归查询算法,有更高的查询效率。
我们首先将多级数据按照下面的方式画在纸上,在根节点Food的左侧写上 1 然后沿着这个树继续向下 在 Fruit 的左侧写上 2 然后继续前进,沿着整个树的边缘给每一个节点都标上左侧和右侧的数字。最后一个数字是标在Food 右侧的 18。 在下面的这张图中你可以看到整个标好了数字的多级结构。(没有看懂?用你的手指指着数字从1数到18就明白怎么回事了。还不明白,再数一遍,注意移动你的手指)。 
这些数字标明了各个节点之间的关系,"Red"的号是3和6,它是 "Food" 1-18 的子孙节点。 同样,我们可以看到 所有左值大于2和右值小于11的节点 都是"Fruit" 2-11 的子孙节点 

如图所示:


id="iframe_0.3956111944689287" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/images/blogjava_net/lijie250/sitepoint_numbering.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.3956111944689287',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 411px; height: 166px;">

这样整个树状结构可以通过左右值来存储到数据库中。继续之前,我们看一看下面整理过的数据表。 


id="iframe_0.12679756591786906" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/images/blogjava_net/lijie250/table02.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.12679756591786906',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 243px; height: 193px;">


注意:由于"left"和"right"在 SQL中有特殊的意义,所以我们需要用"lft"和"rgt"来表示左右字段。 另外这种结构中不再需要"parent"字段来表示树状结构。也就是 说下面这样的表结构就足够了。 

SELECT * FROM tree WHERE lft BETWEEN 2 AND 11;

id="iframe_0.9347072559898306" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/images/blogjava_net/lijie250/table03.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.9347072559898306',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 243px; height: 130px;">


看到了吧,只要一个查询就可以得到所有这些节点。为了能够像上面的递归函数那样显示整个树状结构,我们还需要对这样的查询进行排序。用节点的左值进行排序: 

SELECT * FROM tree WHERE lft BETWEEN 2 AND 11 ORDER BY lft ASC;

那么某个节点到底有多少子孙节点呢?很简单,子孙总数=(右值-左值-1)/2 
descendants = (right – left - 1) / 2 ,如果不是很清楚这个公式,那就去翻下书,我们在上数据结构写的很清楚!

添加同一层次的节点的方法如下:

id="iframe_0.09912518670111892" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.09912518670111892',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 11px; height: 16px;"> LOCK  TABLE  nested_category WRITE;
id="iframe_0.6142689961686767" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.6142689961686767',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;">
id="iframe_0.6465608034337575" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.6465608034337575',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;">
id="iframe_0.03268563663667523" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.03268563663667523',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;"> SELECT   @myRight  : =  rgt  FROM  nested_category
id="iframe_0.5977017653969638" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.5977017653969638',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;"> WHERE  name  =   ' Cherry ' ;
id="iframe_0.7807411838472988" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.7807411838472988',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;">
id="iframe_0.654170226401638" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.654170226401638',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;">
id="iframe_0.306397830145853" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.306397830145853',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;">
id="iframe_0.9730142963623956" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.9730142963623956',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;"> UPDATE  nested_category  SET  rgt  =  rgt  +   2   WHERE  rgt  >   @myRight ;
id="iframe_0.038942052098969615" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.038942052098969615',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;"> UPDATE  nested_category  SET  lft  =  lft  +   2   WHERE  lft  >   @myRight ;
id="iframe_0.02188265808386336" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.02188265808386336',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;">
id="iframe_0.9711055783140579" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.9711055783140579',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;"> INSERT   INTO  nested_category(name, lft, rgt)  VALUES ( ' Strawberry ' @myRight   +   1 @myRight   +   2 );
id="iframe_0.46740953966224663" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.46740953966224663',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;">
id="iframe_0.90574700001531" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.90574700001531',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;">UNLOCK TABLES;


添加树的子节点的方法如下:

id="iframe_0.6125719491608155" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.6125719491608155',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;"> LOCK  TABLE  nested_category WRITE;
id="iframe_0.8086349572503271" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.8086349572503271',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;">
id="iframe_0.5721404730677422" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.5721404730677422',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;"> SELECT   @myLeft  : =  lft  FROM  nested_category
id="iframe_0.3585093479067891" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.3585093479067891',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;">
id="iframe_0.6684437827190106" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.6684437827190106',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;"> WHERE  name  =   ' Beef ' ;
id="iframe_0.3501990991174966" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.3501990991174966',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;">
id="iframe_0.9365623916265451" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.9365623916265451',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;"> UPDATE  nested_category  SET  rgt  =  rgt  +   2   WHERE  rgt  >   @myLeft ;
id="iframe_0.1314463978158129" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.1314463978158129',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;"> UPDATE  nested_category  SET  lft  =  lft  +   2   WHERE  lft  >   @myLeft ;
id="iframe_0.20358801031290064" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.20358801031290064',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;">
id="iframe_0.7292002204944861" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.7292002204944861',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;"> INSERT   INTO  nested_category(name, lft, rgt)  VALUES ( ' charqui ' @myLeft   +   1 @myLeft   +   2 );
id="iframe_0.9202787346076766" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.9202787346076766',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;">
id="iframe_0.21755254588587358" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.21755254588587358',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;">UNLOCK TABLES;


每次插入节点之后都可以用以下SQL进行查看验证:

id="iframe_0.061181992401268426" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.061181992401268426',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;"> SELECT  CONCAT( REPEAT(  '   ' , ( COUNT (parent.name)  -   1 ) ), node.name)  AS  name
id="iframe_0.9235198274569945" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.9235198274569945',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;"> FROM  nested_category  AS  node,
id="iframe_0.16680421396751455" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.16680421396751455',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;">nested_category  AS  parent
id="iframe_0.05780234059690548" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.05780234059690548',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;"> WHERE  node.lft  BETWEEN  parent.lft  AND  parent.rgt
id="iframe_0.4140674460515581" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.4140674460515581',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;"> GROUP   BY  node.name
id="iframe_0.22243325377101675" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.22243325377101675',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;"> ORDER   BY  node.lft;
id="iframe_0.9092998927949045" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.9092998927949045',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;">


删除节点的方法,稍微有点麻烦是有个中间变量,如下:

id="iframe_0.2520466532980443" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.2520466532980443',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;"> LOCK  TABLE  nested_category WRITE;
id="iframe_0.6397932982207271" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.6397932982207271',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;">
id="iframe_0.646321145362138" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.646321145362138',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;">
id="iframe_0.4452144871550052" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.4452144871550052',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;"> SELECT   @myLeft  : =  lft,  @myRight  : =  rgt,  @myWidth  : =  rgt  -  lft  +   1
id="iframe_0.09339217230071761" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.09339217230071761',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;"> FROM  nested_category
id="iframe_0.43439248519327744" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.43439248519327744',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;"> WHERE  name  =   ' Cherry ' ;
id="iframe_0.4724060126863574" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.4724060126863574',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;">
id="iframe_0.5263351787737991" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.5263351787737991',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;">
id="iframe_0.45816751107322484" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.45816751107322484',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;"> DELETE   FROM  nested_category  WHERE  lft  BETWEEN   @myLeft   AND   @myRight ;
id="iframe_0.7759752687035879" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.7759752687035879',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;">
id="iframe_0.7694774409009302" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.7694774409009302',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;">
id="iframe_0.45432717870899353" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.45432717870899353',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;"> UPDATE  nested_category  SET  rgt  =  rgt  -   @myWidth   WHERE  rgt  >   @myRight ;
id="iframe_0.07764505274784184" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.07764505274784184',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;"> UPDATE  nested_category  SET  lft  =  lft  -   @myWidth   WHERE  lft  >   @myRight ;
id="iframe_0.5049228875848566" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.5049228875848566',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;">
id="iframe_0.403842581457605" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=2842637%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.403842581457605',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;">UNLOCK TABLES;

 

这种方式就是有点难的理解,但是适合数据量很大规模使用,查看所有的结构只需要两条SQL语句就可以了,在添加节点和删除节点的时候略显麻烦,不过相对于效率来说还是值得的,这次发现让我发现了数据库结构真的很有用,但是我在学校学的树基本上都忘记了,这次遇到这个问题才应用到项目中!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值