mysql 查詢所有父級名稱


SELECT T2.id, T2.name 
FROM ( 
    SELECT 
        @r AS _id, 
        @stop:=@stop+if(@r=2,1,@stop) as stop,
        (SELECT @r := p_id FROM goods_class WHERE id = _id) AS p_id, 
        @l := @l + 1 AS lvl 
    FROM 
        (SELECT @r := 39, @l := 0, @stop:=0) vars, 
        goods_class h 
    WHERE @stop < 1) T1 
JOIN goods_class T2 
ON T1._id = T2.id 
ORDER BY T1.lvl DESC 

 


免責聲明!

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



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