ClosureTable直译过来叫闭包表？不过不重要，ClosureTable以一张表存储节点之间的关系、其中包含了任何两个有关系（上下级）节点的关联信息

ClosureTable演示

ancestor 祖先：上级节点的id
descendant 子代：下级节点的id
distance 距离：子代到祖先中间隔了几级

4.1 子节点查询

``SELECT descendant FROM CategoryTree WHERE ancestor=5 AND distance=1``

``SELECT descendant FROM CategoryTree WHERE ancestor=5 AND distance>0``

4.2 路径查询

``SELECT ancestor FROM CategoryTree WHERE descendant=10 ORDER BY distance DESC``

``````SELECT ancestor FROM CategoryTree WHERE descendant=10 AND
distance<(SELECT distance FROM CategoryTree WHERE descendant=10 AND ancestor=3)
ORDER BY distance DESC``````

4.3 查询节点所在的层级（深度）

``SELECT distance FROM CategoryTree WHERE descendant=5 AND ancestor=0``

``SELECT distance FROM CategoryTree WHERE descendant=5 AND ancestor=10``

4.4 查询某一层的所有节点

``SELECT descendant FROM CategoryTree WHERE ancestor=0 AND distance=3``

4.5 插入

``INSERT INTO CategoryTree(ancestor,descendant,distance) (SELECT ancestor,10,distance+1 FROM CategoryTree WHERE descendant=5)``

``INSERT INTO CategoryTree(ancestor,descendant,distance) VALUES(10,10,0)``

4.6 移动

``DELETE FROM CategoryTree WHERE descendant=5``