首页 >数据库 >mysql教程 ><转>mysql 树查询话语

<转>mysql 树查询话语

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB原创
2016-06-07 16:14:501202浏览

转mysql 树查询语句 原链接:http://skudy.iteye.com/blog/1434254 求下级所有节点 SELECT b.* FROM tree AS a, tree AS b WHERE a.id= b.pid AND(a.id= ? OR a.pi= ?) 求所有子节点 SELECT * FROM tree t WHERE NOT EXISTS( SELECT 1 FROM tree t1, tree t2

mysql 树查询语句
原链接:http://skudy.iteye.com/blog/1434254
求下级所有节点


SELECT  
    b.*  
FROM  
    tree AS a,  
    tree AS b  
WHERE  
    a.id= b.pid  
AND(a.id= ? OR a.pi= ?) 



求所有子节点

SELECT  
    *  
FROM  
    tree t  
WHERE  
    NOT EXISTS(  
        SELECT  
            1  
        FROM  
            tree  t1,  
            tree t2  
        WHERE  
            t1.id= t2.pid  
        AND t1.id= t.id  
    ) 



求所有根节点


SELECT  
    *  
FROM  
    tree t  
WHERE  
    NOT EXISTS(  
        SELECT  
            1  
        FROM  
            tree t1,  
            tree t2  
        WHERE  
            t1.id= t2.pid  
        AND t1.id= t.pid  
    )  

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn