导航

SQL 递归

Posted on 2017-03-09 14:43  _eve  阅读(195)  评论(0编辑  收藏  举报
向上  
WITH RECURSIVE r AS (  
SELECT * FROM acu_customercircle   
WHERE circleunitid = 'd05c24fa-dc32-4357-8a48-0f72efdfd85a'  union ALL SELECT   
  
acu_customercircle.* FROM acu_customercircle,  
r WHERE acu_customercircle.circleunitid = r.guidparentid)  
SELECT * FROM r ORDER BY circleunitid;  
  
向下  
WITH RECURSIVE T (circleunitid, strname, guidparentid, dwgrade)  AS (  
    SELECT circleunitid, strname, guidparentid, dwgrade  
    FROM acu_customercircle  
    WHERE circleunitid = '712a9d32-e7d9-41d7-b07f-42a2e8c4a827'  
    UNION ALL  
    SELECT  D.circleunitid, D.strname, D.guidparentid, D.dwgrade  
    FROM acu_customercircle D  
    JOIN T ON D.guidparentid = T.circleunitid)  
    SELECT circleunitid, strname, guidparentid, dwgrade FROM T  
ORDER BY dwgrade;