Mysql递归查询

SELECT
IFNULL(CONCAT('[',GROUP_CONCAT(CONCAT('{"catId":"',t.id,'","catName":"',t.name_ch,'","catLevel":',t.level,'}')),']'),"[]") AS companyCategories
FROM
(
SELECT t2.id, t2.name_en,t2.name_ch,t2.level
FROM ( 
    SELECT 
        @r AS _id, 
        (SELECT @r := parent_id FROM c_company_catalog WHERE id = _id) AS parent_id, 
        @l := @l + 1 AS lvl 
    FROM 
        (SELECT @r := 2595, @l := 0) vars, 
        c_company_catalog
    WHERE @r <> 0) t1 
JOIN c_company_catalog t2 
ON t1._id = t2.id 
ORDER BY t1.lvl DESC ) t

你可能感兴趣的:(Mysql)