mysql查詢所有子節點 非遞歸


mysql查詢所有子節點 #查葉子節點 #查孩子節點

不用代碼遞歸

不用存儲過程,臨時表,只用單純的sql語句

SELECT
    t3.college_code 
FROM
    (
    SELECT
    t1.college_code,
IF
    ( find_in_set( t1.parent_org_code, @pids ) > 0, @pids := concat( @pids, ',', t1.college_code ), 0 ) AS ischild 
FROM
    ( SELECT college_code, parent_org_code FROM t_college t WHERE t.STATUS = 1 ORDER BY parent_org_code, college_code ) t1,
    ( SELECT @pids := '22300' college_code ) t2 
    ) t3 
WHERE
    t3.ischild != 0





-- 表結構:  t_college:      id,   college_code (機構編碼),   parent_org_code (父機構編碼)

大家把上面的

college_code 換成你的code,
parent_org_code 換成父節點code,
t_college 換成你的表名


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM