--通过子节点查询父节点WITH TREE AS( 
    SELECT * FROM tstructure 
    WHERE id = 6  -- 要查询的子 id 
    UNION ALL 
    SELECT tstructure.* FROM tstructure, TREE 
    WHERE TREE.parent = tstructure.id
) 
SELECT * FROM TREE--通过父节点查询子节点WITH TREE AS( 
    SELECT * FROM tstructure 
    WHERE parent = 2  -- 要查询的父 id 
    UNION ALL 
    SELECT tstructure.* FROM tstructure, TREE 
    WHERE tstructure.parent = TREE.id
) 
SELECT * FROM TREE

复制代码

来一个我现实中用到的代码吧

复制代码

(    
    .   url     
     
  ..  ., tree    TREE.fid  .    tree   code

复制代码

摘:http://www.cnblogs.com/cracker/archive/2012/07/07/2580781.html