MySQL 获取子分类ID的所有父分类ID和Name的集合

1 CREATE DEFINER=`sa`@`%` PROCEDURE `proc_Product_leimu_ParentIds`(

2     IN pID INT

3 )

4 BEGIN

5 SELECT T2.ID, T2.Name FROM (  SELECT @r AS _id, (SELECT @r := ParentID FROM product_leimu WHERE ID = _id) AS ParentID, @l := @l + 1 AS lvl FROM (SELECT @r := pID, @l := 0) vars, product_leimu h WHERE @r <> 0) T1  

6 JOIN product_leimu T2 ON T1._id = T2.ID ORDER BY T1.lvl DESC;

7 END
MySQL 获取子分类ID的所有父分类ID和Name的集合

 

你可能感兴趣的:(mysql)